MuPAD Pro Computing Essentials, 2nd Edition

The quadratic equation procedure

Here is a procedure obtained from the previous example. It is doing exactly the same what the previous example was doing, but uses the procedure declaration syntax.

quadEquation := proc(a,b,c)
begin
   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);
         return(x1, x2)
      else
         print("No real solutions")
      end
   else
      if b<>0 then
         x1 := -c/b;
         return(x1)
      else
         print("No solutions")
      end
   end
end_proc:

Now  we can use this procedure like here:

quadEquation(23,1,-100);

quadEquation(1,1,-31)

 

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