MuPAD Pro Computing Essentials, 2nd Edition

Example 4.1 The quadratic equation

This is basic example showing how to use  the if statement and one of the key examples for learning how to develop simple algorithms. Note, for the simplicity of the algorithm we do not consider  here all cases that we should consider while solving quadratic equations.

a := 1:
b := -21:
c := 3:
if a<>0 then
   delta := b^2 - 4*a*c;
   if delta >= 0 then
      x1 := (-b + sqrt(delta))/(2*a);
      x2 := (-b - sqrt(delta))/(2*a);
      print(Typeset, x1, x2)
   else
      print("No real solutions")
   end_if
else
   if b<>0 then
      x1 := -c/b
   else
      print("No solutions")
   end_if
end_if
 

 

© Miroslaw Majewski, Abu Dhabi,  Update  26-10-2004