error when plotting 3 first order differential equations
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Jon
il 27 Set 2023
Risposto: Walter Roberson
il 27 Set 2023
I get the error seen in the middle when trying to plot the 3 first order differential equations, what am I entering incorrectly? I included the problem and my work and code, what is this error;
Index in position 2 exceeds array bounds. Index must not exceed 1.
Error in indexing (line 968)
R_tilde = builtin('subsref',L_tilde,Idx); and how or what do I do to get it to solve with out any errors?
e) Re-write the 3rd order differential equation as a system of three first-order differential equations.
Solution:
e) Use the MATLAB ode45() function to solve the system of equations and plot your solution on a new figure. The result provided by ode45() and your solution from above should match.
clear
clc
syms x;
soln = ode45(@Problem03ODEFunction,[0 10],[0;0;0]);
y = x(:, 1); % y(t)
yd = x(:, 2); % y'(t)
ydd = x(:, 3); %y"(t)
figure;
plot(soln.x,soln.y(1,:),'-','linewidth',1);
hold on;
plot(soln.x,soln.y(2,:),':','linewidth',1);
plot(soln.x,soln.y(3,:),'--','LineWidth',1);
grid on;
title('Problem 3 ODE45 Solution');
xlabel('Time (s)');
ylabel('Solution');
legend('x_{1}(t)','x_{2}(t)','x_3{3}(t)','location','best');
function [dXdt] = Problem03ODEFunction(t,x)
dx1dt = x(2);
dx2dt = x(3);
dx3dt= - (19/12) * x(3) - (19/24) * x(2) - (1/8) * x(1) + f(t);
[dXdt]=[dx1dt;dx2dt;dx3dt];
function ft=f(t)
if t>=1 && t<10
ft=1;
else
ft=0;
end
end
end
0 Commenti
Risposta accettata
Walter Roberson
il 27 Set 2023
syms x;
You created x as a scalar symbolic variable.
soln = ode45(@Problem03ODEFunction,[0 10],[0;0;0]);
You calculate the solution to an ode problem, assigning the solution to soln
y = x(:, 1); % y(t)
You index the scalar symbolic variable x, which you have not changed anywhere in your code. In particular, x is not the output from the ode45 call.
When you use a single output for ode45() it outputs a solution object, which you would manipulate with tools such as deval
if t>=1 && t<10
ft=1;
else
ft=0;
end
The mathematics of ode45 and all Runge Kutta methods, requires that the first two derivatives of the provided expressions must be continuous. If you are lucky then ode45() will notice and give you an error message. If you are not lucky, such as in your code, then ode45() will simply give you the wrong results without warning you that your output is nonsense.
You need to stop the first ode45() call at t = 10 seconds, and take the output you get and input it to a second ode45() call that uses the 0 for ft for the rest of the time. That is relatively easy for this case as the boundaries are strictly time-based: just be careful with what tspan you pass in. In the more general case where you need to determine dynamically whether you have encountered a boundary (such is the case for a bouncing ball) then you would need to use event functions.
0 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Ordinary Differential Equations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!