Unable to understand the error

1 回表示 (過去 30 日間)
Mehul kumar
Mehul kumar 2021 年 11 月 19 日
回答済み: Akash Singh 2021 年 12 月 7 日
close all
clc
syms c1 c2 x m omega gamma Fo
F=input('enter the coefficients [a,b,c]:');
f=input('enter the RHS function f(x):');
a=F(1);b=F(2);c=F(3);
AE=a*m^2+b*m+c;
m=solve(AE);
m1=m(1);m2=m(2);
D=b^2-4*a*c;
if(D>0)
y1=exp(m1*x);
y2=exp(m2*x);
elseif (D==0)
y1=exp(m1*x);y2=x*exp(m1*x);
else
alfa=real(m1);beta=imag(m1);
y1=exp(alfa*x)*cos(beta*x);
y2=exp(alfa*x)*sin(beta*x);
end
yc=c1*y1+c2*y2;
fx=f/a;
W=y1*diff(y2,x)-y2*diff(y1,x);
u=int(-y2*fx/W,x);
v=int(y1*fx/W,x);
yp=y1*u+y2*v;
y_gen=yc+yp;
check=input('If the problem has initial conditions then enter 1 else enter 2:');
if(check==1)
cn=input('Enter the initial conditions[x0,y(x0),Dyx(0)]:');
dy_gen=diff(y_gen);
eq1=(subs(y_gen,x,cn(1))-cn(2));
eq2=(subs(dy_gen,x,cn(1))-cn(3));
[c1,c2]=solve(eq1,eq2);
y=simplify(subs(y_gen));
disp('The complete solution is');
disp(y);
fplot(y,[cn(1),cn(1)+2]);
else
y=simplify(y_gen);
disp('The general solution is');
disp(y);
end
ERROR
Conversion to logical from sym is not possible.
Error in onetwothree (line 12)
if(D>0)
  2 件のコメント
KSSV
KSSV 2021 年 11 月 19 日
What inputs have you given? It seems D is syms class, so you cannot use like that.
Mehul kumar
Mehul kumar 2021 年 11 月 19 日
inputs are
[1,0,omega*omega]
Fo*sin(gamma*x)
[0,0]

サインインしてコメントする。

回答 (1 件)

Akash Singh
Akash Singh 2021 年 12 月 7 日
As you mentioned your input is [1,0,omega*omega] which makes value of a, b, c as 1, 0 and omega*omega respectively.
Omega is a sym, which makes c a sym.
In this line of code
D=b^2-4*a*c;
D is also sym
Please note, sym is not a function. It will not evaluate and assign a result to D.
if(D>0)
So, when you compare D(sym) with 0 (double), system throws error because sym can’t be compared to a numeric value.
If you really want to compare D with 0, you will have to first substitute the value of 'omega' in D. The way to do that is by using subs, which will substitute the value.
You can do something like -
if(double(subs(D, yourvalue))>0)
Please refer a similar answered question below -

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by