Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

about if statement and solution

Asked by Siva Malla on 31 May 2012

Dear sir,

when I run this bellow code, it shows this error,

code:

syms x
a=solve(x^2+4*x+4);
b=a(1)
if b>2
    s=2
else
    s=3
end

error:

Undefined function or method 'gt' for input arguments of type 'sym'.
Error in ==> solution at 4
if b>2

my target is, I have to compare the value of 'x' in the equation with constant. but I got this error.

please help me the solution for my problem..

0 Comments

Siva Malla

Tags

Products

No products are associated with this question.

1 Answer

Answer by Walter Roberson on 31 May 2012
Accepted answer

If you want numeric answers, work numerically:

a = roots([1, 4, 4]);

solve() does not return numbers: it returns formula, some of which happen to print out like numbers. If you happen to get a solve() result that involves only known functions of constants, then you can use double() on the result to calculate the double-precision approximation of the result.

By the way, you should not assume that the results of solve() are returned in any particular order.

4 Comments

Walter Roberson on 31 May 2012

When f is a vector, the test "if f>2" is the same as "if all(f >2)" . Is that what you want? Or do you want to test "if any(f > 2)" ?

When I tested the formula using some arbitrary constants, I found only one solution, not two: with those particular constants at least, one side of the discontinuity was strictly positive but the other side crossed 0.

Walter Roberson on 31 May 2012

In any case, test double(f) not f itself.

Siva Malla on 1 Jun 2012

Sir,

I have to extract first f1, f2....fn in the vector f, means in all values of f, and I have to test any value of f. like if f2>2; because, first I have to take only real values of f and only I should test these real values of f to 2,( any real value of f). this way I want.

here I am giving complete my program please test. in this program, I cant work with if statement. so please rectify this problem and please help me.

program:

L=1.0*1e-4;
a=0.15*1e-4;
W=10*1e-4;
q=1.6*1e-19;
Es=85.845*1e-14;
Nd=5*1e17;
Nc=1.83*1e19;
Nv=4.07*1e19;
Nref=2*1e17;
KT=0.0259;
q1=1;
h1=0.98;
Umin=40;
Umax=950;
r=0.76
x1=1;
vsat=2.07*1e7;
k1=q*Nd*W*a*x1*vsat;
Vbi=0.8-((KT*q1)*log(Nc/Nd));
U0= Umin + ((Umax - Umin)/(1+ ((Nd/Nref)^r)));
G0=(q*Nd*U0*W*a)/L;
Vp=(q*Nd*a^2)/(2*Es);
Ip=G0*Vp/3;
Z=(q*Nd*a^2*U0)/(2*Es*L*vsat);
Ec=vsat*(h1/(1-h1))/U0;
VG=-8
for k=1:8
Vd=1:20;
a1=((Vbi-VG)/Vp)^0.5;
syms x
for i=1:20
b=x^2-a1^2;
c=h1*(1-x);
d=2*(x^3-a1^3)/3;
e=L*Z*((b-d)/c-b);
y=Vp*b+2*Ec*a*x/pi*sinh(pi*(L-e)/(2*a*x))-Vd(i);
f=solve(y);
f
VL1=Vp*(f^2-a1^2);
end
VG=VG+1;
if Vd(i)>VL1
w=2;
else
w=3;
end
end

Walter Roberson

Contact us