Invalid indexing for dsolve
Show older comments
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)
Star Strider
on 12 Jan 2021
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
matlab_day
on 12 Jan 2021
Star Strider
on 13 Jan 2021
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')
.
Star Strider
on 13 Jan 2021
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!
Star Strider
on 13 Jan 2021
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.
matlab_day
on 13 Jan 2021
Star Strider
on 13 Jan 2021
Just use the code I posted in my previous Comment and delete everything prior to that, or run it in a new script.
matlab_day
on 13 Jan 2021
Star Strider
on 13 Jan 2021
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.
matlab_day
on 14 Jan 2021
Star Strider
on 14 Jan 2021
There is a singularity (±Inf) value created at that point. This is a problem with the differential equation coding or the initial conditions.
matlab_day
on 14 Jan 2021
Star Strider
on 14 Jan 2021
That was demonstration code.
Use your own initial conditions in each iteration of the loop.
matlab_day
on 14 Jan 2021
Star Strider
on 14 Jan 2021
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.)
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!