macros can be made to work like subs, you just need to know a few tricks to get it to work the same way.  macros just works in a slightly different manner and we can make it useful.

The difference is with subs, one has to keep specifying the substitution with each equation you want subbed, whereas macro will already have it defined.  As an example:

a := v^2*z^3 - 34/(5*x^2*sin(y*v^2)) + 36*v^2 - b*v^2 + 3^(v^2 - cos(v^2 + g))
                            

If we want to substitute h for v^2, then we would normally do this using subs

subs(v^2=h,a)
                          

however, we can also use macro

macro(v^2=h)
                                    

now it doesn't just automatically substitute those values so we need to coax maple a little bit.  We can do that by converting the equation to a string and parsing it.

parse(convert(a,string))
                     

so as you see we arrive at the same result.  Now there is a caveat using macro, if you've already defined a variable in a macro, subs will not work using the same variable sustitution - you first need to reset the variable in the macro back to itself. 

subs(v^2=h,a)
                      #doesn't work since the variable is defined in a macro

macro(v^2=v^2) #reset the variable in the macro

subs(v^2=h,a)
                         # now it works

we could also define a little procedure to simplify our typing, to have the macro variable work on our equation.

mvs:=proc(a) #macro variable substitution
  parse(convert(a,string));
end proc:

macro(v^2=h)
mvs(a)
            

now if we had some other existing equation before defining the macro
aa:=exp(v^2-sin(theta))+v^2*cos(theta)-1/x^sin(v^2-g)
                                   

we just have to simply apply our proc on the equation to apply the variable substitution
mvs(aa)
              


 

 

 

 


Please Wait...