Can somebody tell me why I am getting an error while using the subs function?

조회 수: 1 (최근 30일)
IC=input('Enter the initial conditions in the form [y(0),Dy(0)]'); y(0)=IC(1); dy0=IC(2); LTY= subs(LTY, {'laplace(y(t), t, s)','y(0)','D(y)(0)'}, {Y,y0,dy0});

채택된 답변

Steven Lord
Steven Lord 2021년 5월 28일
In general when asking for help with an error you should show the full and exact text (everything displayed in red in the Command Window) of the error message you receive when you run the code. But in this case one problem is obvious.
y(0)=IC(1);
There's no such thing as element 0 of an array in MATLAB. From the context of the rest of the code I think you wanted to write:
y0=IC(1);
There may be other problems, and if there are please show us the contents of the LTY and Y variables as well as what you entered at the input prompt for the initial conditions.
  댓글 수: 1
Prabhat Ranjan
Prabhat Ranjan 2021년 5월 28일
편집: Prabhat Ranjan 2021년 5월 28일
clear all
clc
syms t s y(t) Y
dy(t)=diff(y(t));
d2y(t)=diff(y(t),2);
F = input('Input the coefficients [a,b,c]: ');
a=F(1);b=F(2);c=F(3);
nh = input('Enter the non-homogenous part f(x): ');
eqn=a*d2y(t)+b*dy(t)+c*y(t)-nh;
LTY=laplace(eqn,t,s);
IC = input('Enter the initial conditions in the form [y0,Dy(0)]: ');
y0=IC(1);
dy0=IC(2);
LTY=subs(LTY,{'laplace(y(t), t, s)','y(0)','D(y)(0)'},{Y,y0,dy0});
eq=collect(LTY,Y);
Y=simplify(solve(eq,Y));
yt=simplify(ilaplace(Y,s,t));
disp('The solution of the differential equation y(t)=') disp(yt);
ezplot(yt,[y0,y0+2]);
This is the whole code. I am getting an error in the 14th line that is the subs function. And apologies for the inconvenience since I'm asking for the 1st time!
This is the error which I am getting:
Error using sym>convertChar (line 1580)
Character vectors and strings in the first argument can only specify a variable or number. To evaluate character vectors and strings representing symbolic expressions, use 'str2sym'.
Error in sym>tomupad (line 1296)
S = convertChar(x);
Error in sym (line 234)
S.s = tomupad(x);
Error in sym/subs>@(x)sym(x) (line 175)
X = cellfun(@(x)sym(x),X,'UniformOutput',false);
Error in sym/subs>normalize (line 175)
X = cellfun(@(x)sym(x),X,'UniformOutput',false);
Error in sym/subs>mupadsubs (line 166)
[X2,Y2,symX,symY] = normalize(X,Y); %#ok
Error in sym/subs (line 154)
G = mupadsubs(F,X,Y);
Error in HEhe (line 14)
LTY=subs(LTY,{'laplace(y(t), t, s)','y(0)','D(y)(0)'},{Y,y0,dy0});

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numbers and Precision에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by