Conditions

0 means false; any non-zero number means true

In [2]:
if false
    disp('True condition');
end
In [4]:
if 1
    disp('True condition');
else
    disp('False condition');
end
True condition

The roots of quadratic equations $a x^2 + b x + c = 0$ are $ r = \frac{-b \pm \sqrt{b^2 - 4ac}}{2a}$.

In [6]:
a=1; b=0; c=-3;
d=b^2-4*a*c
if d>=0
    disp('roots are positive.');
end
d =

    12

roots are positive.
In [7]:
a=1; b=1; c=3;
d=b^2-4*a*c
if d>=0
    disp('roots are positive.');
else
    disp('roots are immaginary');
end
d =

   -11

roots are immaginary
In [8]:
a=1; b=1; c=3;
d=b^2-4*a*c
if d>0
    disp('roots are positive and distinct');
    r1 = (-b+sqrt(d))/(2*a)
    r2 = (-b-sqrt(d))/(2*a)
elseif d==0
    disp('roots are real and same');
    r = -b/(2*a);
else
    disp('roots are immaginary');
    re = -b/(2*a); im = sqrt(-d)/(2*a);
    fprintf('r1 = %f + i %f \n',re, im);
    fprintf('r2 = %f - i %f \n',re, im);
end
d =

   -11

roots are immaginary
r1 = -0.500000 + i 1.658312 
r2 = -0.500000 - i 1.658312
In [6]:
%plot gnuplot
In [1]:
% define a quadretic function with a, b and c parameters
f = @(x, a, b, c) a*x.^2 + b*x + c;
x= -3:0.1:3;
y1=f(x,1,1,-1); %we get two real roots 
y2=f(x,1,-2,1); %one real root
y3=f(x,1,1,1); %no real root
plot(x,y1,'r',x,y2,'g',x,y3,'b')
hold on
plot(x,0*x,'k',0*ones(length(x),1),10*x,'k') %plotting axises
hold off
axis([-3, 3, -5,20])
In [10]:
x=1
while x<=10
    x=2*x
end
x =

     1


x =

     2


x =

     4


x =

     8


x =

    16
In [ ]: