Preben Alsholm

MaplePrimes Activity


These are replies submitted by Preben Alsholm

@pagan Excellent!

Singularities to the left could be included as well, and a message printed out if so desired.

sol:=dsolve({diff(f(x),x)=1/x/(x+2), f(-1)=1/10},numeric,output=listprocedure):
 

g:=proc(x)
   try
      sol(x);
   catch "cannot evaluate the solution further right of %1, probably a singularity","cannot evaluate the solution further left of %1, probably a singularity":
   StringTools:-FormatMessage(lastexception[2..-1])
   end try;
end proc:

g(1);
g(-3);

Instead of explicitly inputting the strings substitution could be made like this:

sol(1);
lastexception;
s1:=lastexception[2];
sol(-3);
s2:=lastexception[2];
#Here the construction of g is done in two steps for clarity. g1 doesn't work, of course.
g1:=proc(x)
   try
      sol(x);
   catch "STRING":
   StringTools:-FormatMessage(lastexception[2..-1])
   end try;
 end proc:
g:=subs("STRING"=(s1,s2),eval(g1)):
g(1);
g(-3);

@pagan Excellent!

Singularities to the left could be included as well, and a message printed out if so desired.

sol:=dsolve({diff(f(x),x)=1/x/(x+2), f(-1)=1/10},numeric,output=listprocedure):
 

g:=proc(x)
   try
      sol(x);
   catch "cannot evaluate the solution further right of %1, probably a singularity","cannot evaluate the solution further left of %1, probably a singularity":
   StringTools:-FormatMessage(lastexception[2..-1])
   end try;
end proc:

g(1);
g(-3);

Instead of explicitly inputting the strings substitution could be made like this:

sol(1);
lastexception;
s1:=lastexception[2];
sol(-3);
s2:=lastexception[2];
#Here the construction of g is done in two steps for clarity. g1 doesn't work, of course.
g1:=proc(x)
   try
      sol(x);
   catch "STRING":
   StringTools:-FormatMessage(lastexception[2..-1])
   end try;
 end proc:
g:=subs("STRING"=(s1,s2),eval(g1)):
g(1);
g(-3);

As Psedomodo says, you don't need to assign the values to the names in order to use them. In most cases I wouldn't want to do that.

In any case you could use an elementwise operation like this:

The list SUBS can be used by subs or eval:

SUBS:=convert(store[..,1]=~store[..,2],list);
#Simultaneous substitutions will be made when one list or one set of substitutions is used and is the only valid syntax for 'eval'. The success of sequential substitution, which is an option in 'subs', is dependent on the proper order in the sequence. Thus notice that
subs(SUBS,S1); #CX3 not substituted since not seen
subs(op(SUBS),S1); #CX3 still not substituted: Wrong order in the sequence.
subs(SUBS,S1):  subs(SUBS,%); #In this case two uses of 'subs' does it.
#You could solve for a1, CX3, and S1, then there will be no problem with simultaneous substitution using either 'subs' or 'eval':

store[..,1]=~store[..,2];
SUBS2:=solve(convert(%,list),indets(store) minus {x});
subs(SUBS2,S1);#OK
eval(S1,SUBS2);#OK

#If you prefer assignment then you can use the two elementwise operations assign~ and  =~  :
assign~(store[..,1]=~store[..,2]):
a1,CX3,S1;


As Psedomodo says, you don't need to assign the values to the names in order to use them. In most cases I wouldn't want to do that.

In any case you could use an elementwise operation like this:

The list SUBS can be used by subs or eval:

SUBS:=convert(store[..,1]=~store[..,2],list);
#Simultaneous substitutions will be made when one list or one set of substitutions is used and is the only valid syntax for 'eval'. The success of sequential substitution, which is an option in 'subs', is dependent on the proper order in the sequence. Thus notice that
subs(SUBS,S1); #CX3 not substituted since not seen
subs(op(SUBS),S1); #CX3 still not substituted: Wrong order in the sequence.
subs(SUBS,S1):  subs(SUBS,%); #In this case two uses of 'subs' does it.
#You could solve for a1, CX3, and S1, then there will be no problem with simultaneous substitution using either 'subs' or 'eval':

store[..,1]=~store[..,2];
SUBS2:=solve(convert(%,list),indets(store) minus {x});
subs(SUBS2,S1);#OK
eval(S1,SUBS2);#OK

#If you prefer assignment then you can use the two elementwise operations assign~ and  =~  :
assign~(store[..,1]=~store[..,2]):
a1,CX3,S1;


@PatrickT Just a minor comment: Warnings do come in connection with singularities but seem to come from some plotting procedures:

restart;
sol:=dsolve({diff(f(x),x)=1/x, f(-1)=1/10},numeric,output=listprocedure):
sol(1); #Error
F:=subs(sol,f(x));
plot(F,-1..1); #No warnings nor errors reported
DEtools[DEplot](diff(f(x),x)=1/x,f(x),x=-1..1,[[f(-1)=1/10]]); #Warning about error!
plots:-odeplot(sol,[x,f(x)],-1..1); #Warning

@PatrickT Just a minor comment: Warnings do come in connection with singularities but seem to come from some plotting procedures:

restart;
sol:=dsolve({diff(f(x),x)=1/x, f(-1)=1/10},numeric,output=listprocedure):
sol(1); #Error
F:=subs(sol,f(x));
plot(F,-1..1); #No warnings nor errors reported
DEtools[DEplot](diff(f(x),x)=1/x,f(x),x=-1..1,[[f(-1)=1/10]]); #Warning about error!
plots:-odeplot(sol,[x,f(x)],-1..1); #Warning

@PatrickT My point was that  catch:  catches all exceptions no matter what you write after the colon.

Thus in the following three examples the error is caught, but only in the first case is the message of any use.

a:="56";
try
sin(a)
catch: StringTools:-FormatMessage(lastexception[2..-1]);
45
end try;

a:="56";
try
sin(a)
catch: StringTools:-FormatMessage("Once upon a time ... ");
45
end try;

a:="56";
try
sin(a)
catch: "Once upon a time ... ";
45
end try;

@PatrickT My point was that  catch:  catches all exceptions no matter what you write after the colon.

Thus in the following three examples the error is caught, but only in the first case is the message of any use.

a:="56";
try
sin(a)
catch: StringTools:-FormatMessage(lastexception[2..-1]);
45
end try;

a:="56";
try
sin(a)
catch: StringTools:-FormatMessage("Once upon a time ... ");
45
end try;

a:="56";
try
sin(a)
catch: "Once upon a time ... ";
45
end try;

@PatrickT I think that

try
.....
catch:
.....
end try;

catches all errors (not warnings!).

The addition of StringTools:-FormatMessage after the colon just has the effect of printing the message. If you want the relevant message printed you can use

StringTools:-FormatMessage(lastexception[2..-1]);

Example:

a:="56";
try
sin(a)
catch: StringTools:-FormatMessage(lastexception[2..-1]);
45
end try;

@PatrickT I think that

try
.....
catch:
.....
end try;

catches all errors (not warnings!).

The addition of StringTools:-FormatMessage after the colon just has the effect of printing the message. If you want the relevant message printed you can use

StringTools:-FormatMessage(lastexception[2..-1]);

Example:

a:="56";
try
sin(a)
catch: StringTools:-FormatMessage(lastexception[2..-1]);
45
end try;

@samiyare 

Try

fsolve(p(-1.2, pp2) = 0, pp2 = -10 .. 10);
plot(p(-1.2, pp2) , pp2 = 1..1.15);
fsolve(p(-1.2, pp2) = 0, pp2 = 1.05);


@samiyare 

Try

fsolve(p(-1.2, pp2) = 0, pp2 = -10 .. 10);
plot(p(-1.2, pp2) , pp2 = 1..1.15);
fsolve(p(-1.2, pp2) = 0, pp2 = 1.05);


@koonduck Save the plots and use 'display'.

odeplot(dsol, [x, theta(x)], 0 .. 40);
p1 := %;
Omega := sqrt(omega^2-(g/(omega*R))^2);
#It is inadvisable to use theta and theta[a] in the same worksheet, so I have used another name:
thetaapprox := x->(1/3)*Pi+cos(Omega*x);
#You need an 'x' in the plotting interval when plotting an expression containing x. When plotting a procedure, however, you should only have the interval itself.
plot(thetaapprox(x), x = 0 .. 40, color = blue); p2:=%:
display(p1, p2);

If thetaapprox is supposed to be an approximation then certainly the amplitude is way off!

@koonduck Save the plots and use 'display'.

odeplot(dsol, [x, theta(x)], 0 .. 40);
p1 := %;
Omega := sqrt(omega^2-(g/(omega*R))^2);
#It is inadvisable to use theta and theta[a] in the same worksheet, so I have used another name:
thetaapprox := x->(1/3)*Pi+cos(Omega*x);
#You need an 'x' in the plotting interval when plotting an expression containing x. When plotting a procedure, however, you should only have the interval itself.
plot(thetaapprox(x), x = 0 .. 40, color = blue); p2:=%:
display(p1, p2);

If thetaapprox is supposed to be an approximation then certainly the amplitude is way off!

Although this method is very fast, it seems to run into problems when the degree is 10000:

restart;
F:=randpoly(epsilon,degree=10000,dense):
simplify(F, {epsilon^2=1});

Error, (in solve_eqn) too many levels of recursion

Added: It seems that the revised version SE2 of my proposed procedure SE above is faster than using simplify/siderels.

First 200 201 202 203 204 205 206 Last Page 202 of 231