Misaw
Misaw

Reputation: 11

Maple procedure and function

I don't understand how to use a mathematical function in a maple procedure.
For example in this procedure I try to compute the arc length of a function :

proc(f,a,b);
local f,a,b;
L=int(sqrt(1+f'(x)^2),x=a..b);
end proc;

But is doesn't work.
It is my first question here if there is a problem

Upvotes: 0

Views: 650

Answers (1)

acer
acer

Reputation: 7246

Firstly, the syntax f'(x) is only valid in Maple's marked up 2S Math, not in plaintext Maple code. So it does not make sense to show us that syntax as code on this site.

Moving on, and using only 1D plaintext code (which makes sense for this site and is, in my opinion, a better choice for learning Maple), you need to understand the difference between procedures and expresssions.

An expressiom and differentiating it:

Fexpr := x^2 + sin(x);

               2         
     Fexpr := x  + sin(x)

diff(Fexpr, x);

         2 x + cos(x)

An operator, and differentiating it (and applying it, or applying it after differentiating):

F := t -> t^2 + sin(t);

     F := t -> t^2 + sin(t)

D(F);

        t -> 2*t + cos(t)

D(F)(x);

          2 x + cos(x)

F(x);

               2         
              x  + sin(x)

diff(F(x), x);

             2 x + cos(x)

Here are some examples:

Your task, done with argument f as an expression, and supplying the variable name as an argument:

restart;
myproc := proc(fexpr,a,b,var)
  local L;
  L:=int(sqrt(1+diff(fexpr,var)^2),var=a..b);
  end proc:

myproc(sin(x), 0, Pi, x);
myproc(sin(x), 0.0, evalf(Pi), x);
myproc(sin(t), 0.0, evalf(Pi), t);
myproc(t^2, 0, Pi, t);

A modification of the above, where it figures out the variable name from the expression passed as argument:

restart;
myproc := proc(fexpr,a,b)
  local deps,L,x;
  deps := indets(fexpr,
                 And(name,Not(constant),
                     satisfies(u->depends(fexpr,u))));
  x := deps[1];
  L:=int(sqrt(1+diff(fexpr,x)^2),x=a..b);
end proc:

myproc(sin(x), 0, Pi);
myproc(sin(x), 0.0, evalf(Pi));
myproc(sin(t), 0.0, evalf(Pi));
myproc(t^2, 0, Pi);

Now, significantly different, passing an procedure (operator) instead of an expression:

restart;
myproc := proc(f,a,b)
  local L,x;
  L:=int(sqrt(1+D(f)(x)^2),x=a..b);
end proc:

myproc(sin, 0, Pi);
myproc(sin, 0.0, evalf(Pi));
myproc(s -> s^2, 0, Pi);

Lastly (and I do not recommend this way, since the procedure f may not function properly when called with an unassigned name, etc):

restart;
myproc := proc(f,a,b)
  local L,x;
  L:=int(sqrt(1+diff(f(x),x)^2),x=a..b);
end proc:

myproc(sin, 0, Pi);
myproc(sin, 0.0, evalf(Pi));
myproc(s -> s^2, 0, Pi);

Upvotes: 1

Related Questions