Invalid indexing for dsolve
2 次查看(过去 30 天)
显示 更早的评论
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!
0 个评论
回答(1 个)
Star Strider
2021-1-12
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 个评论
Star Strider
2021-1-14
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.)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!