Preben Alsholm

MaplePrimes Activity


These are replies submitted by Preben Alsholm

@PatrickT Using one of the ways of finding the dimension of a vector given by acer you could do like this

TaylorExpand := proc(F,X::name,n::posint) local d,i;
 d:=op(1,F(X));
 mtaylor~(F(X),[[seq(X[i],i=1..d)]$d],n)
end proc;

As much as I hate 2d-input I appreciate the effort to make input look like mathematical expressions the way they are usually written.

Thus I appreciate very much that you can define a function f, so that f(2) returns the value of f at 2. In your comment are you somehow trying to say that the use of procedures is ineffective or slow and that the use of eval is faster or more effective?

(I should note that I too have seen students define procedures in situations where they never make use of any other input than, say x.)

As much as I hate 2d-input I appreciate the effort to make input look like mathematical expressions the way they are usually written.

Thus I appreciate very much that you can define a function f, so that f(2) returns the value of f at 2. In your comment are you somehow trying to say that the use of procedures is ineffective or slow and that the use of eval is faster or more effective?

(I should note that I too have seen students define procedures in situations where they never make use of any other input than, say x.)

Your formulation suggests that this is a homework problem or similar. Am I mistaken?

@Wang Gaoteng If a tilde ~ follows a procedure (or a function) then that procedure works elementwise on a 'container' object like a vector, an array, a list, a set, or a table.

Thus e.g.   f~([1,2,3,4]); results in [f(1), f(2), f(3), f(4)].

You could have used map of course.

map is more general, but the tilde-elementwise operation is very convenient, when container objects are involved.

@Wang Gaoteng If a tilde ~ follows a procedure (or a function) then that procedure works elementwise on a 'container' object like a vector, an array, a list, a set, or a table.

Thus e.g.   f~([1,2,3,4]); results in [f(1), f(2), f(3), f(4)].

You could have used map of course.

map is more general, but the tilde-elementwise operation is very convenient, when container objects are involved.

@Kamel Algeria

OK, what I mean is that solve doesn't make any real use of the  assumptions. `assuming` is sent to work, but solve does not in general make any use of the assumptions.

simplify(x) assuming diff(f(x),x)>0, diff(diff(f(x),x),x)<0;
`assuming`([x],[diff(f(x),x)>0, diff(diff(f(x),x),x)<0]);

@Kamel Algeria

OK, what I mean is that solve doesn't make any real use of the  assumptions. `assuming` is sent to work, but solve does not in general make any use of the assumptions.

simplify(x) assuming diff(f(x),x)>0, diff(diff(f(x),x),x)<0;
`assuming`([x],[diff(f(x),x)>0, diff(diff(f(x),x),x)<0]);

@Kamel Algeria Try this

f:=x->sin(x^3+4):
res:=solve(f(x)=0,x) assuming diff(f(x),x)>0, diff(diff(f(x),x),x)<0:
seq(eval(diff(f(x),x)>0,x=res[i]),i=1..3):
evalc([%]);

the output is

[0 < 6*2^(1/3), 0 < -3*2^(1/3)-(3*I)*2^(1/3)*sqrt(3), 0 < -3*2^(1/3)+(3*I)*2^(1/3)*sqrt(3)]

Only the first result satisfies f´(x) >0. The other two don't satisfy that requirement (and the requirement doesn't make any sense for imaginary results). So assumptions were not being used.

@Kamel Algeria Try this

f:=x->sin(x^3+4):
res:=solve(f(x)=0,x) assuming diff(f(x),x)>0, diff(diff(f(x),x),x)<0:
seq(eval(diff(f(x),x)>0,x=res[i]),i=1..3):
evalc([%]);

the output is

[0 < 6*2^(1/3), 0 < -3*2^(1/3)-(3*I)*2^(1/3)*sqrt(3), 0 < -3*2^(1/3)+(3*I)*2^(1/3)*sqrt(3)]

Only the first result satisfies f´(x) >0. The other two don't satisfy that requirement (and the requirement doesn't make any sense for imaginary results). So assumptions were not being used.

@Kamel Algeria In your example the result is the same if you don't use assumptions.

In the help page for solve it says (emphasis added):

"If the output of the solve command is a piecewise-defined expression, then the assuming command can be used to isolate the desired solution(s). If the output is not piecewise-defined, in particular, if the output is constant, assumptions on the independent variables may be ignored. If there are parameters in the input equations, the solve command will use those assumptions in its computations."

@Kamel Algeria In your example the result is the same if you don't use assumptions.

In the help page for solve it says (emphasis added):

"If the output of the solve command is a piecewise-defined expression, then the assuming command can be used to isolate the desired solution(s). If the output is not piecewise-defined, in particular, if the output is constant, assumptions on the independent variables may be ignored. If there are parameters in the input equations, the solve command will use those assumptions in its computations."

It would help tremendously if you would provide us with the lines of code you have used, so we don't stumble around in the dark.

@goli As I mentioned earlier, you could turn your equation into a differential equation. That I have done at the end of this comment.

If you use fsolve, then add a starting value to ensure that you get the positive branch. I have chosen H=1 (it shouldn't be too crucial, which positive value you choose, but don't make it too small).

restart;
with(plots):
eq := z-> (H^2+(-1)*.27*(1+z)^3-(1/20000)*(1+z)^4)/(H^2)^.1 = .7299500000;
Y := z->fsolve(eq(z), H=1):
plot(Y, -2.2 .. 5);
implicitplot(eq(z), z = -3 .. 10,H=-10..10,gridrefine=2);
plot(fdiff(Y, [1], z), z = -2.2 .. 6, caption = "The derivative of y using fdiff on Y");
yp := implicitdiff(eq(z), H, z);
plot(eval(yp, H = 'Y(z)'), z = -2.2 .. 6, caption = "The derivative of y using implicitdiff and Y");
plot(eval((1+z)*yp/H, H = 'Y(z)'), z = -1 .. 5);

#The differential equation approach:

ode:=diff(H(z),z)=subs(H=H(z),yp);
eval(eq(0),H=1);
res:=dsolve({ode,H(0)=1},numeric,output=listprocedure);
odeplot(res,[z,H(z)],-2.22..5);
Ynum:=subs(res,H(z));
Ynum(2.345678);


@goli As I mentioned earlier, you could turn your equation into a differential equation. That I have done at the end of this comment.

If you use fsolve, then add a starting value to ensure that you get the positive branch. I have chosen H=1 (it shouldn't be too crucial, which positive value you choose, but don't make it too small).

restart;
with(plots):
eq := z-> (H^2+(-1)*.27*(1+z)^3-(1/20000)*(1+z)^4)/(H^2)^.1 = .7299500000;
Y := z->fsolve(eq(z), H=1):
plot(Y, -2.2 .. 5);
implicitplot(eq(z), z = -3 .. 10,H=-10..10,gridrefine=2);
plot(fdiff(Y, [1], z), z = -2.2 .. 6, caption = "The derivative of y using fdiff on Y");
yp := implicitdiff(eq(z), H, z);
plot(eval(yp, H = 'Y(z)'), z = -2.2 .. 6, caption = "The derivative of y using implicitdiff and Y");
plot(eval((1+z)*yp/H, H = 'Y(z)'), z = -1 .. 5);

#The differential equation approach:

ode:=diff(H(z),z)=subs(H=H(z),yp);
eval(eq(0),H=1);
res:=dsolve({ode,H(0)=1},numeric,output=listprocedure);
odeplot(res,[z,H(z)],-2.22..5);
Ynum:=subs(res,H(z));
Ynum(2.345678);


First 215 216 217 218 219 220 221 Last Page 217 of 229