You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN.
1 view (last 30 days)
Show older comments
I have been getting the error "Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN."
In the '.xlsx' sheet i have bunch of numbers that have been taken from a cfd software. Can anyone help me with this one why I am getting this error? (I think the error might be due to the excel file only but am not sure since the file contain just a list of numbers)
Here is the complete code:
Main code:
clear all
clc
global K M C u;
Ne=6;
l=1; %length
t=0.02; %thickness
b=0.02; %width
modulus=2e11; %(E)
area=b*t;
imoment=(b*((t)^3))/12;
Le=l/Ne; %length of element
Rho=7850; %density
%Element stiffness matrix
K1=(modulus*imoment/(Le^3))*[12,6*Le,-12,6*Le; ...
6*Le,4*Le*Le,-6*Le,2*Le*Le; ...
-12,-6*Le,12,-6*Le; ...
6*Le,2*Le*Le,-6*Le,4*Le*Le];
Kglobal=zeros(2*(Ne+1),2*(Ne+1));
M1=[156 22*Le 54 -13*Le;...
22*Le 4*Le*Le 13*Le -3*Le*Le;...
54 13*Le 156 -22*Le;...
-13*Le -3*Le*Le -22*Le 4*Le*Le]*(Rho*Le*b*t)/420;
Mglobal=zeros(2*(Ne+1),2*(Ne+1));
for ii=1:Ne
Kglobal(2*ii-1:2*(ii+1),2*ii-1:2*(ii+1))=Kglobal(2*ii-1:2*(ii+1),2*ii-1:2*(ii+1))+K1;
Mglobal(2*ii-1:2*(ii+1),2*ii-1:2*(ii+1))=Mglobal(2*ii-1:2*(ii+1),2*ii-1:2*(ii+1))+M1;
end
K=Kglobal;
K(1:2,:)=[];
K(:,1:2)=[];
M=Mglobal;
M(1:2,:)=[];
M(:,1:2)=[];
C=0.05*Kglobal;
C(1:2,:)=[];
C(:,1:2)=[];
K
M
C
u=(2*Ne)+1;
dt=0.001;
T=300;
%Displacement initials
y0=zeros(2*(2*(Ne+1))-4,1);
y0(end-1,1)=0.5;
%ODE function
t_array = xlsread('l&d.xlsx','Q1:Q300'); % This is t array from xls file
f_array = xlsread('l&d.xlsx','O1:O300'); % This is F array from xls file
[tsol ysol]=ode15s(@(t, y) beam_function(t, y, t_array, f_array),[1:dt:T],y0);
plot(tsol,ysol(:,Ne))
Function code:
function [dy]=beam_function(t,y, t_array, f_array)
F = interp1(t_array,f_array,t);
dy=[y(1:u-1);
inv(M)*(F-K*y(u:end)-C*y(1:u-1))]
Accepted Answer
Walter Roberson
on 21 Aug 2021
Inside your function the following variables are not defined: C, K, M, u
Global variables are only accessible inside functions that declare them as global (or inside nested functions of a function that declared them global.)
Avoid using global.
Note: there is no point in taking inv(M) inside the function, as you do not change M inside the function. Either use M\(F-K*y(u:end)-C*y(1:u-1)) or else calculate inv(M) outside and pass in the inverse.
Note: using interp1() with no interpolation method specified results in linear interpolation. However, linear interpolation of a variable results in the derivative of F being discontinuous, which is something that will cause problems with the ode*() routines, as the ode*() routines rely upon the second derivative of all statements to be continuous. If you must interpolate, then use 'spline' method.
10 Comments
Bhanu Pratap Akherya
on 21 Aug 2021
Now, I used the spline method for interpolation but the error shown now is "Not enough input arguments."
I don't know what that means can you explain that please?
Walter Roberson
on 21 Aug 2021
Most likely you coded
F = interp1(t_array,f_array,t,spline);
when you neeed to have coded
F = interp1(t_array,f_array,t,'spline');
Walter Roberson
on 21 Aug 2021
format long g
Ne=6;
l=1; %length
t=0.02; %thickness
b=0.02; %width
modulus=2e11; %(E)
area=b*t;
imoment=(b*((t)^3))/12;
Le=l/Ne; %length of element
Rho=7850; %density
%Element stiffness matrix
K1=(modulus*imoment/(Le^3))*[12,6*Le,-12,6*Le; ...
6*Le,4*Le*Le,-6*Le,2*Le*Le; ...
-12,-6*Le,12,-6*Le; ...
6*Le,2*Le*Le,-6*Le,4*Le*Le];
Kglobal=zeros(2*(Ne+1),2*(Ne+1));
M1=[156 22*Le 54 -13*Le;...
22*Le 4*Le*Le 13*Le -3*Le*Le;...
54 13*Le 156 -22*Le;...
-13*Le -3*Le*Le -22*Le 4*Le*Le]*(Rho*Le*b*t)/420;
Mglobal=zeros(2*(Ne+1),2*(Ne+1));
for ii=1:Ne
Kglobal(2*ii-1:2*(ii+1),2*ii-1:2*(ii+1))=Kglobal(2*ii-1:2*(ii+1),2*ii-1:2*(ii+1))+K1;
Mglobal(2*ii-1:2*(ii+1),2*ii-1:2*(ii+1))=Mglobal(2*ii-1:2*(ii+1),2*ii-1:2*(ii+1))+M1;
end
K=Kglobal;
K(1:2,:)=[];
K(:,1:2)=[];
M=Mglobal;
M(1:2,:)=[];
M(:,1:2)=[];
C=0.05*Kglobal;
C(1:2,:)=[];
C(:,1:2)=[];
K
K = 12×12
13824000 0 -6912000 576000 0 0 0 0 0 0 0 0
0 128000 -576000 32000 0 0 0 0 0 0 0 0
-6912000 -576000 13824000 0 -6912000 576000 0 0 0 0 0 0
576000 32000 0 128000 -576000 32000 0 0 0 0 0 0
0 0 -6912000 -576000 13824000 0 -6912000 576000 0 0 0 0
0 0 576000 32000 0 128000 -576000 32000 0 0 0 0
0 0 0 0 -6912000 -576000 13824000 0 -6912000 576000 0 0
0 0 0 0 576000 32000 0 128000 -576000 32000 0 0
0 0 0 0 0 0 -6912000 -576000 13824000 0 -6912000 576000
0 0 0 0 0 0 576000 32000 0 128000 -576000 32000
rank(K)
ans =
12
M
M = 12×12
0.388761904761905 0 0.0672857142857143 -0.00269973544973545 0 0 0 0 0 0 0 0
0 0.00027689594356261 0.00269973544973545 -0.000103835978835979 0 0 0 0 0 0 0 0
0.0672857142857143 0.00269973544973545 0.388761904761905 0 0.0672857142857143 -0.00269973544973545 0 0 0 0 0 0
-0.00269973544973545 -0.000103835978835979 0 0.00027689594356261 0.00269973544973545 -0.000103835978835979 0 0 0 0 0 0
0 0 0.0672857142857143 0.00269973544973545 0.388761904761905 0 0.0672857142857143 -0.00269973544973545 0 0 0 0
0 0 -0.00269973544973545 -0.000103835978835979 0 0.00027689594356261 0.00269973544973545 -0.000103835978835979 0 0 0 0
0 0 0 0 0.0672857142857143 0.00269973544973545 0.388761904761905 0 0.0672857142857143 -0.00269973544973545 0 0
0 0 0 0 -0.00269973544973545 -0.000103835978835979 0 0.00027689594356261 0.00269973544973545 -0.000103835978835979 0 0
0 0 0 0 0 0 0.0672857142857143 0.00269973544973545 0.388761904761905 0 0.0672857142857143 -0.00269973544973545
0 0 0 0 0 0 -0.00269973544973545 -0.000103835978835979 0 0.00027689594356261 0.00269973544973545 -0.000103835978835979
rank(M)
ans =
12
C
C = 12×12
691200 0 -345600 28800 0 0 0 0 0 0 0 0
0 6400 -28800 1600 0 0 0 0 0 0 0 0
-345600 -28800 691200 0 -345600 28800 0 0 0 0 0 0
28800 1600 0 6400 -28800 1600 0 0 0 0 0 0
0 0 -345600 -28800 691200 0 -345600 28800 0 0 0 0
0 0 28800 1600 0 6400 -28800 1600 0 0 0 0
0 0 0 0 -345600 -28800 691200 0 -345600 28800 0 0
0 0 0 0 28800 1600 0 6400 -28800 1600 0 0
0 0 0 0 0 0 -345600 -28800 691200 0 -345600 28800
0 0 0 0 0 0 28800 1600 0 6400 -28800 1600
rank(C)
ans =
12
u=(2*Ne)+1;
dt=0.001;
T=300;
%Displacement initials
y0=zeros(2*(2*(Ne+1))-4,1);
y0(end-1,1)=0.5;
%ODE function
t_array = xlsread('l&d.xlsx','Q1:Q300'); % This is t array from xls file
Error using xlsread (line 136)
Unable to open file 'l&d.xlsx'.
File '/users/mss.system.M0BtCI/l&d.xlsx' not found.
Unable to open file 'l&d.xlsx'.
File '/users/mss.system.M0BtCI/l&d.xlsx' not found.
f_array = xlsread('l&d.xlsx','O1:O300'); % This is F array from xls file
[tsol ysol]=ode15s(@(t, y) beam_function(t, y, t_array, f_array, C, K, M, u),[1:dt:T],y0);
plot(tsol,ysol(:,Ne))
function [dy]=beam_function(t,y, t_array, f_array)
F = interp1(t_array,f_array,t,'spline');
dy=[y(1:u-1);
M\(F-K*y(u:end)-C*y(1:u-1))]
end
Bhanu Pratap Akherya
on 21 Aug 2021
thanks for your help the issue was exactly as you told. But the original error is still showing up: "Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN." could it be due to the .xlsx file?
Walter Roberson
on 22 Aug 2021
Edited: Walter Roberson
on 22 Aug 2021
I am not seeing it in R2021a.
The current code is
filename = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/718084/l&d.xlsx';
format long g
Ne=6;
l=1; %length
t=0.02; %thickness
b=0.02; %width
modulus=2e11; %(E)
area=b*t;
imoment=(b*((t)^3))/12;
Le=l/Ne; %length of element
Rho=7850; %density
%Element stiffness matrix
K1=(modulus*imoment/(Le^3))*[12,6*Le,-12,6*Le; ...
6*Le,4*Le*Le,-6*Le,2*Le*Le; ...
-12,-6*Le,12,-6*Le; ...
6*Le,2*Le*Le,-6*Le,4*Le*Le];
Kglobal=zeros(2*(Ne+1),2*(Ne+1));
M1=[156 22*Le 54 -13*Le;...
22*Le 4*Le*Le 13*Le -3*Le*Le;...
54 13*Le 156 -22*Le;...
-13*Le -3*Le*Le -22*Le 4*Le*Le]*(Rho*Le*b*t)/420;
Mglobal=zeros(2*(Ne+1),2*(Ne+1));
for ii=1:Ne
Kglobal(2*ii-1:2*(ii+1),2*ii-1:2*(ii+1))=Kglobal(2*ii-1:2*(ii+1),2*ii-1:2*(ii+1))+K1;
Mglobal(2*ii-1:2*(ii+1),2*ii-1:2*(ii+1))=Mglobal(2*ii-1:2*(ii+1),2*ii-1:2*(ii+1))+M1;
end
K=Kglobal;
K(1:2,:)=[];
K(:,1:2)=[];
M=Mglobal;
M(1:2,:)=[];
M(:,1:2)=[];
C=0.05*Kglobal;
C(1:2,:)=[];
C(:,1:2)=[];
K
K = 12×12
13824000 0 -6912000 576000 0 0 0 0 0 0 0 0
0 128000 -576000 32000 0 0 0 0 0 0 0 0
-6912000 -576000 13824000 0 -6912000 576000 0 0 0 0 0 0
576000 32000 0 128000 -576000 32000 0 0 0 0 0 0
0 0 -6912000 -576000 13824000 0 -6912000 576000 0 0 0 0
0 0 576000 32000 0 128000 -576000 32000 0 0 0 0
0 0 0 0 -6912000 -576000 13824000 0 -6912000 576000 0 0
0 0 0 0 576000 32000 0 128000 -576000 32000 0 0
0 0 0 0 0 0 -6912000 -576000 13824000 0 -6912000 576000
0 0 0 0 0 0 576000 32000 0 128000 -576000 32000
rank(K)
ans =
12
M
M = 12×12
0.388761904761905 0 0.0672857142857143 -0.00269973544973545 0 0 0 0 0 0 0 0
0 0.00027689594356261 0.00269973544973545 -0.000103835978835979 0 0 0 0 0 0 0 0
0.0672857142857143 0.00269973544973545 0.388761904761905 0 0.0672857142857143 -0.00269973544973545 0 0 0 0 0 0
-0.00269973544973545 -0.000103835978835979 0 0.00027689594356261 0.00269973544973545 -0.000103835978835979 0 0 0 0 0 0
0 0 0.0672857142857143 0.00269973544973545 0.388761904761905 0 0.0672857142857143 -0.00269973544973545 0 0 0 0
0 0 -0.00269973544973545 -0.000103835978835979 0 0.00027689594356261 0.00269973544973545 -0.000103835978835979 0 0 0 0
0 0 0 0 0.0672857142857143 0.00269973544973545 0.388761904761905 0 0.0672857142857143 -0.00269973544973545 0 0
0 0 0 0 -0.00269973544973545 -0.000103835978835979 0 0.00027689594356261 0.00269973544973545 -0.000103835978835979 0 0
0 0 0 0 0 0 0.0672857142857143 0.00269973544973545 0.388761904761905 0 0.0672857142857143 -0.00269973544973545
0 0 0 0 0 0 -0.00269973544973545 -0.000103835978835979 0 0.00027689594356261 0.00269973544973545 -0.000103835978835979
rank(M)
ans =
12
C
C = 12×12
691200 0 -345600 28800 0 0 0 0 0 0 0 0
0 6400 -28800 1600 0 0 0 0 0 0 0 0
-345600 -28800 691200 0 -345600 28800 0 0 0 0 0 0
28800 1600 0 6400 -28800 1600 0 0 0 0 0 0
0 0 -345600 -28800 691200 0 -345600 28800 0 0 0 0
0 0 28800 1600 0 6400 -28800 1600 0 0 0 0
0 0 0 0 -345600 -28800 691200 0 -345600 28800 0 0
0 0 0 0 28800 1600 0 6400 -28800 1600 0 0
0 0 0 0 0 0 -345600 -28800 691200 0 -345600 28800
0 0 0 0 0 0 28800 1600 0 6400 -28800 1600
rank(C)
ans =
12
u=(2*Ne)+1;
dt=0.001;
T=300;
%Displacement initials
y0=zeros(2*(2*(Ne+1))-4,1);
y0(end-1,1)=0.5;
%ODE function
t_array = readmatrix(filename, 'range', 'Q1:Q300'); % This is t array from xls file
f_array = readmatrix(filename, 'range', 'O1:O300'); % This is F array from xls file
[tsol, ysol]=ode15s(@(t, y) beam_function(t, y, t_array, f_array, C, K, M, u),[1:dt:T],y0);
plot(tsol,ysol(:,Ne))
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/718144/image.png)
function [dy]=beam_function(t,y, t_array, f_array, C, K, M, u)
F = interp1(t_array,f_array,t,'spline');
dy=[y(1:u-1);
M\(F-K*y(u:end)-C*y(1:u-1))];
end
Bhanu Pratap Akherya
on 22 Aug 2021
did you get the result on 2021a? can please share what were the results?
More Answers (0)
See Also
Categories
Find more on Special Functions in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)