Invalid indexing for dsolve

I am trying to make a plot of 2 ODE'S x(t), y(t) with the following code as explained in (https://www.mathworks.com/help/symbolic/solve-a-system-of-differential-equations.html):
syms x(t) y(t)
ode1 = diff(x) == x(t)*y(t) + 0.5*x(t)^3 + x(t)*y(t)^2;
ode2 = diff(y) == -y(t) - 2*x(t)^2 + x(t)^2*y(t);
odes = [ode1; ode2];
cond1 = x(0) == 0;
cond2 = y(0) == 1;
conds = [cond1; cond2];
[xSol(t), ySol(t)] = dsolve(odes,conds);
fplot(xSol)
hold on
fplot(ySol)
grid on
legend('uSol','vSol','Location','best')
However, I ge the following message: "In myODE (line 10)
Error using sym/subsindex (line 857)
Invalid indexing or function definition. Indexing must follow
MATLAB indexing. Function arguments must be symbolic
variables, and function body must be sym expression."
I'm not sure why there is an error since it matches the website I linked above. Thank you!

Answers (1)

The equations are nonlinear, and for most nonlinear differential equations, an analytic solution does not exist.
Try this instead:
syms x(t) y(t) T Y
ode1 = diff(x) == x(t)*y(t) + 0.5*x(t)^3 + x(t)*y(t)^2;
ode2 = diff(y) == -y(t) - 2*x(t)^2 + x(t)^2*y(t);
odes = [ode1; ode2];
cond1 = x(0) == 0;
cond2 = y(0) == 1;
conds = [cond1; cond2];
[VF,Subs] = odeToVectorField(odes);
ODEfcn = matlabFunction(VF, 'Vars',{T,Y});
[t, Sol] = ode45(ODEfcn, [0 1], [1 0]+eps);
figure
yyaxis left
plot(t, Sol(:,1))
yyaxis right
plot(t, Sol(:,2))
grid on
legend(string(Subs),'Location','N')
.

14 Comments

Thank you for your answer! Can you tell me how I would plot several curves with differenti intial conditions onto the same plot?
My pleasure!
Try this:
tv = linspace(1,10,50); % Choose Desired Time Vector Or Range For All Plots
for k = 1:10
ic = rand(1,2); % Set Initial Conditions (Use Your Own Set) & Change For Each Iteration
[tc{k}, Sol{k}] = ode45(ODEfcn, tv, ic); % Integrate & Store Results
end
figure
hold on
for k = 1:numel(Sol)
yyaxis left
plot(tc{k}, Sol{k}(:,1), 'DisplayName',sprintf('y_{%2d}', k))
yyaxis right
plot(tc{k}, Sol{k}(:,2), 'DisplayName',sprintf('x_{%2d}', k))
end
grid
hold off
legend('Location','eastoutside')
.
matlab_day’s Answer moved to this Comment —
When I compiled the following code:
syms x(t) y(t) T Y
ode1 = diff(x) == x(t)*y(t) + 0.5*x(t)^3 + x(t)*y(t)^2;
ode2 = diff(y) == -y(t) - 2*x(t)^2 + x(t)^2*y(t);
odes = [ode1; ode2];
cond1 = x(0) == .1;
cond2 = y(0) == .5;
conds = [cond1; cond2];
[VF,Subs] = odeToVectorField(odes);
ODEfcn = matlabFunction(VF, 'Vars',{T,Y});
[t, Sol] = ode45(ODEfcn, [0 1], [1 0]+eps);
tv = linspace(1,10,50);
for k = 1:10
ic = rand(1,2);
[tc{k}, Sol{k}] = ode45(ODEfcn, tv, ic);
end
figure
hold on
for k = 1:numel(Sol)
yyaxis left
plot(tc{k}, Sol{k}(:,1), 'DisplayName',sprintf('y_{%2d}', k))
yyaxis right
plot(tc{k}, Sol{k}(:,2), 'DisplayName',sprintf('x_{%2d}', k))
end
grid
hold off
legend('Location','eastoutside')
It returned the error "Unable to perform assignment because brace indexing is not supported for variables of this type.
Error in myODE (line 17)
[tc{k}, Sol{k}] = ode45(ODEfcn, tv, ic);"
I'm not sure if this is how you intended that i integrated your code into mine. Thank you!
You first have to copy:
ODEfcn = @(T,Y)[Y(2).^2.*-2.0+Y(1).*Y(2).^2-Y(1);Y(2).^3./2.0+Y(1).^2.*Y(2)+Y(1).*Y(2)]
from the Command Window, and put it just before the ‘tv’ assignment:
ODEfcn = @(T,Y)[Y(2).^2.*-2.0+Y(1).*Y(2).^2-Y(1);Y(2).^3./2.0+Y(1).^2.*Y(2)+Y(1).*Y(2)];
tv = linspace(1,10,50); % Choose Desired Time Vector
for k = 1:10
ic = rand(1,2); % Set Initial Conditions
[tc{k}, Sol{k}] = ode45(ODEfcn, tv, ic); % Integrate & Store Results
end
figure
hold on
for k = 1:numel(Sol)
yyaxis left
plot(tc{k}, Sol{k}(:,1), 'DisplayName',sprintf('y_{%2d}', k))
yyaxis right
plot(tc{k}, Sol{k}(:,2), 'DisplayName',sprintf('x_{%2d}', k))
end
grid
hold off
legend('Location','eastoutside')
Then it works as it is supposed to.
Hello, Thank you for your response. It still says "Unable to perform assignment because brace indexing is not supported for variables of this type.
Error in myODE (line 18)
[tc{k}, Sol{k}] = ode45(ODEfcn, tv, ic);"
The line number went from 17 to 18 because of the line you told me to insert. I think the issue may be with ode45 though I am not sure.
Thank you!
Just use the code I posted in my previous Comment and delete everything prior to that, or run it in a new script.
Thank you. I assume the first line
ODEfcn = @(T,Y][Y(2).^2.*-2.0+Y(1).*Y(2).^2-Y(1);Y(2).^3./2.0+Y(1).^2.*Y(2)+Y(1).*Y(2)]
is supposed to define the system of ODE's but I'm wondering why it doens't have an x in it.
My pleasure!
See the ‘Subs’ variable. The ‘x(t)’ function is the second variable, the ‘y(t)’ function is the first variable. The odeToVectorField function makes the appropriate substitutions as elements of the ‘Y’ vector, and matlabFunction creates an appropriate anonymous function using them. That is also the purpose of the legend call, so the resulting plot explains the two variables. Knowing the variable mapping provided by ‘Subs’ also provides the necessary information to construct the initial conditions vector.
Thank you. I received the following error message. I thought it has something to do with the 3rd parameter of linspace being too large but despite decreasing it nothing changed.
n ode45 (line 360)
In myODE (line 20)
Warning: Failure at t=3.576800e+00. Unable to meet integration tolerances without
reducing the step size below the smallest value allowed (7.105427e-15) at time t.
There is a singularity (±Inf) value created at that point. This is a problem with the differential equation coding or the initial conditions.
Based on what you have told me I should mess with the intial conditions a bit. This leads me to wonder what the you mean by the code below:
tv = linspace(1,10,20); % Choose Time Vector
for k = 1:10
ic = rand(1, 2); % Set Initial Conditions
[tc{k}, Sol{k}] = ode45(comb_func, tv, ic); % Integrate & Store Results
end
I don't understand why there is a random number generator in the for loop. I understand that the 3rd parameter of ode45 is the inital condition so I'm having trouble connecting that with the random number generator you have written.
That was demonstration code.
Use your own initial conditions in each iteration of the loop.
Thank you. I changed the initial conditions so that I would have x(0) = -5, -.4,...,.4, .5, and I wanted my interval for my independent variable to be from 0 to 10. When I used the following code:
comb_func = @(t,x)[x(1)*x(2) + 0.5*x(1)^3 + x(1)*x(2)^2; -x(2) - 2*x(1)^2 + x(1)^2*x(2)]; %System of ODES x(t) = x(1), y(t) = x(2)
tspan = [0 10]; % Choose Time Vector
for k = 1:11
ic = -.5 + k*.1; % Set Initial Conditions
[tc{k}, Sol{k}] = ode45(comb_func, tspan, ic); % Integrate & Store Results
end
it spit the error message
Index exceeds the number of array elements (1).
Error in myODE>@(t,x)[x(1)*x(2)+0.5*x(1)^3+x(1)*x(2)^2;-x(2)-2*x(1)^2+x(1)^2*x(2)]
(line 16)
which is weird because this error message doesn't appear when I change nothing else but replace the initial conditions line to the random number generator you had.
You have 2 differential equations, so you need 2 initial conditions.
Try this:
ic = [-.5, k*0.1]; % Set Initial Conditions
That will not throw the same error, however you could still encounter the warning about an infinite result. (I did not test the integration code with those initial conditions, so I cannot determine that.)

Sign in to comment.

Categories

Asked:

on 12 Jan 2021

Commented:

on 14 Jan 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!