Question

In MATLAB, I am trying to build a loop which calls dsolve with different initial conditions in each iteration. To accomplish this, I built this loop:

y0 = -2:0.5:2;
y1 = -2:2:2;

syms y(t)

for i = y1

      for k = y0
       y(t) = dsolve(diff(y,2) + diff(y) - 2*y == t^2 - 4*t + 3,...
           ['y(0) == ',num2str(k)],['Dy(0) == ',num2str(i)])
      end
end

It works through the first iteration, but it fails for the second one with this error:

Error using mupadengine/feval (line 157)
MuPAD error: Error: Invalid equation or initial condition. [ode::splitSys]

Error in dsolve>mupadDsolve (line 325)
T = feval(symengine,'symobj::dsolve',sys,x,options);

Error in dsolve (line 186)
sol = mupadDsolve(args, options);

Error in MWE (line 9)
     y(t) = dsolve(diff(y,2) + diff(y) - 2*y == t^2 - 4*t + 3,...

The command works outside the loop for all the initial conditions I have tried. Inside the loop, the first iteration works, the next one always fails. It does not seem to matter which ones I choose.

What's going on?

Was it helpful?

Solution

You can use different name for the solution

y0 = -2:0.5:2;
y1 = -2:2:2;

syms y(t);
for q = y1
      for k = y0
        ysol(t) = dsolve(diff(y,2) + diff(y) - 2*y == t^2 - 4*t + 3,...
                      ['y(0) == ',num2str(k)],['Dy(0) == ',num2str(q)])
      end
end

I would also change i to q

http://www.mathworks.com/help/matlab/ref/i.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top