How to plot the sum of two numerical solutions obtained by Maple?

132 Views Asked by At

I have two solutions of nonlinear ODE numerically solved by Maple, and I'd like to add them up to plot it. The third line is obviously wrong, since sol1 and sol2 are data, which cannot be simply added together. How could do I this task properly? More generally, how could I plot a multivariate function g(sol1, sol2)? (e.g. for summation, g(sol1,sol2)=sol1+sol2)

sol1 := dsolve({diff(y(x), [x$2])+2*b*(diff(y(x), [x$1]))+
    (1.5*o)^2*sin(y(x)) = g*(1.5*o)^2*cos(o*x), 
    y(0) = 0, (D(y))(0) = 0}, y(x), type = numeric); 

sol2 := dsolve({diff(y(x), [x$2])+2*b*(diff(y(x), [x$1]))+
(1.5*o)^2*sin(y(x)) = g*(1.5*o)^2*cos(o*x), y(0) = 0.1e^(-4), (D(y))(0) = 0}, 
y(x), type = numeric); 

odeplot(sol1+sol2, [x, y(x)], 0 .. 7, color = red);
1

There are 1 best solutions below

1
On BEST ANSWER

I'm not sure what you intend with y(0) = 0.1e^(-4). Do you intend 0.1e-4 = 0.00001 or 0.1*exp(-4) or something else? What you wrote (as plaintext 1D Maple Notation code) gets parsed like (0.1e)^(-4) = 10000.0.

One way to add your two solutions involves using the output=listprocedure option. Below I make up values for g, o, and b since you didn't give them. I use y(0) = 0.1e1 = 1.0 as initial condition for sol2 just because it produces a solution that is visibly distinct from that of sol1.

restart:
g:=-9.98: b:=1.0: o:=1.0:

sol1 := dsolve({diff(y(x), [x$2])+2*b*(diff(y(x), [x$1]))+
    (1.5*o)^2*sin(y(x)) = g*(1.5*o)^2*cos(o*x), 
    y(0) = 0, (D(y))(0) = 0}, y(x), type = numeric,
    output=listprocedure):

Y1:=eval(y(x), sol1):

sol2 := dsolve({diff(y(x), [x$2])+2*b*(diff(y(x), [x$1]))+
    (1.5*o)^2*sin(y(x)) = g*(1.5*o)^2*cos(o*x),
    y(0) = 0.1e1, (D(y))(0) = 0}, y(x), type = numeric,
    output=listprocedure):

Y2:=eval(y(x), sol2):

plots:-display(
   plots:-odeplot(sol1, [x, y(x)], 0 .. 7, color = red),
   plots:-odeplot(sol2, [x, y(x)], 0 .. 7, color = blue) );

enter image description here

plot( Y1+Y2, 0 .. 7, color=black );

enter image description here