Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN.
1 次查看(过去 30 天)
显示 更早的评论
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))]
采纳的回答
Walter Roberson
2021-8-21
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 个评论
Bhanu Pratap Akherya
2021-8-21
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
2021-8-21
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
2021-8-21
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
2021-8-21
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
2021-8-22
编辑:Walter Roberson
2021-8-22
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))
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
2021-8-22
did you get the result on 2021a? can please share what were the results?
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Special Functions 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)