Preben Alsholm

13703 Reputation

22 Badges

20 years, 117 days

MaplePrimes Activity


These are replies submitted by Preben Alsholm

Thank you, indeed it does seem to be more efficient.

I tried in the previous simple example to compute the solution at t = 1 for 10000 values of (A, x0). The ratio between the times spent was roughly 3/1.
The actual test is below.

Preben Alsholm

restart;
ode := diff(x(t),t)=x(t)*(A-x(t)):
ic := x(0)=x0:
ivp:=unapply({ode,ic},A,x0):
sol := (A,x0)->dsolve( ivp(A,x0), numeric):
t0:=time():
for k from 1 to 100 do
  for j from 1 to 100 do
    sol(k,j)(1)
  end do
end do:
time()-t0;
                                   30.389
restart;
ode := diff(x(t),t)=x(t)*(A-x(t)):
ic := x(0)=x0:
sol := dsolve( {ode,ic}, numeric,parameters=[A,x0]):
t0:=time():
for k from 1 to 100 do
  for j from 1 to 100 do
    sol(parameters=[A=k,x0=j]);
    sol(1)
  end do
end do:
time()-t0;
                                    8.783
 

I still haven't seen any need for using the parameters version of dsolve/numeric.

What is wrong with the following?

restart;
ode := diff(x(t),t)=x(t)*(A-x(t)):
ic := x(0)=x0:
ivp:=unapply({ode,ic},A,x0);
sol := (A,x0)->dsolve( ivp(A,x0), numeric):
sol(2,1);
 

Preben Alsholm

When using sum, test[1,j] is evaluated at the indices [1,j] with j being the symbol j, therefore the error message.

You are better off using add, but if you do use sum, then put apostrophes around test[1,j] to ensure that the evaluation taking place before sum just consists in removing the apostrophes.

test:=Matrix([[1,2,3],[4,5,6],[9,8,7]]);
sum('test[1,j]',j=1..3);
add(test[1,j],j=1..3);
 

Preben Alsholm

When using sum, test[1,j] is evaluated at the indices [1,j] with j being the symbol j, therefore the error message.

You are better off using add, but if you do use sum, then put apostrophes around test[1,j] to ensure that the evaluation taking place before sum just consists in removing the apostrophes.

test:=Matrix([[1,2,3],[4,5,6],[9,8,7]]);
sum('test[1,j]',j=1..3);
add(test[1,j],j=1..3);
 

Preben Alsholm

Without actually seeing the system I don't have any other suggestions than the following.

Introduce g1 and g2 as new unknowns, so that the integration of f1 and f2 are done by dsolve directly.

restart;
sys:=diff(f1(t),t)=piecewise(t<=4,3*exp(-2*t),-1),diff(f2(t),t)=sin(t),diff(g1(t),t)=f1(t),diff(g2(t),t)=f2(t);
init:= f1(0)=1, f2(0)=1,g1(0)=0,g2(0)=0:
F:=dsolve({sys,init}, {f1(t), f2(t),g1(t),g2(t)}, numeric,output=listprocedure);
F1,F2,G1,G2:=op(subs(F,[f1(t),f2(t),g1(t),g2(t)]));
G2(5.5)/G1(5.5);
 

About the odeplot command: It doesn't solve your system, dsolve/numeric does.

Preben Alsholm

Without actually seeing the system I don't have any other suggestions than the following.

Introduce g1 and g2 as new unknowns, so that the integration of f1 and f2 are done by dsolve directly.

restart;
sys:=diff(f1(t),t)=piecewise(t<=4,3*exp(-2*t),-1),diff(f2(t),t)=sin(t),diff(g1(t),t)=f1(t),diff(g2(t),t)=f2(t);
init:= f1(0)=1, f2(0)=1,g1(0)=0,g2(0)=0:
F:=dsolve({sys,init}, {f1(t), f2(t),g1(t),g2(t)}, numeric,output=listprocedure);
F1,F2,G1,G2:=op(subs(F,[f1(t),f2(t),g1(t),g2(t)]));
G2(5.5)/G1(5.5);
 

About the odeplot command: It doesn't solve your system, dsolve/numeric does.

Preben Alsholm

You may also want to remove terms of the form 0.*I by using simplify:

s^5+3.236067976*s^4+(1.*10^(-10)*I)*s^4+5.236067975*s^3+(7.*10^(-10)*I)*s^3-(3.*10^(-10)*I)*s+5.236067975*s^2+3.236067977*s+1.000000000 :
fnormal(%);
simplify(%);
 

Preben Alsholm

You may also want to remove terms of the form 0.*I by using simplify:

s^5+3.236067976*s^4+(1.*10^(-10)*I)*s^4+5.236067975*s^3+(7.*10^(-10)*I)*s^3-(3.*10^(-10)*I)*s+5.236067975*s^2+3.236067977*s+1.000000000 :
fnormal(%);
simplify(%);
 

Preben Alsholm

I never use algsubs, but very often use subs and eval. They are both extremely useful, both in their own way.

I suppose algsubs has disappointed me in the past. I don't know if it has gotten better, since I don't use it.

Preben Alsholm

 

I never use algsubs, but very often use subs and eval. They are both extremely useful, both in their own way.

I suppose algsubs has disappointed me in the past. I don't know if it has gotten better, since I don't use it.

Preben Alsholm

 

Well, since the output has to be a sum you need an index, just like a definite integral needs an integration variable.

That index could have been made local by the procedure that produced the sum. However, then you wouldn't have immediate access to it.

Preben Alsholm

 

Well, since the output has to be a sum you need an index, just like a definite integral needs an integration variable.

That index could have been made local by the procedure that produced the sum. However, then you wouldn't have immediate access to it.

Preben Alsholm

 

Download 219_fsolveproblem.mpl
View file details

Are you defining Res as I did?

Did you execute all lines previous to the ones mentioned last?

Preben Alsholm

Download 219_fsolveproblem.mpl
View file details

Are you defining Res as I did?

Did you execute all lines previous to the ones mentioned last?

Preben Alsholm

You may try setting optimalitytolerance:

res:=LSSolve(Res,{x1<=1,x2<=1,x3<=1,x4<=1,x5<=1,x6<=1,x7<=1,x8<=1,x9<=1,x10<=1,T>=Ti},assume=nonnegative,iterationlimit=5000,optimalitytolerance=0.0001);
[0.00517923021408238112, [T = 2782.09143970567812, n = 10.1585637591277820,

  x1 = 0.00872658980230692216, x10 = 0.0748602181855385214,

  x2 = 0.0766666505398920090, x3 = 0.579521666596851892,

  x4 = 0.0000115210794556045040, x5 = 0.0901704459988488805,

  x6 = 0.0852556575745576917, x7 = 0.00577381343641316520,

  x8 = 0.00427475756541988076, x9 = 0.0642723480725257840]]

See the help page: ?Optimization, Options

Preben Alsholm

First 223 224 225 226 227 228 229 Page 225 of 229