Plotting first-order differential equation with initial condition
Afficher commentaires plus anciens
I need to plot the solution curve of the differential equation: y'+ty=t^2 on the matlab program. I was given the intial condition of y(0)=3 and I need to graph it on the interval of [-4,4]. I understand how to find the solution of the differential equation but I don't know how to graph the solution curve.
4 commentaires
KSSV
le 21 Jan 2020
Read about ODE45 to get the solution and read about plot to get the graph.
Erin Manogaran
le 21 Jan 2020
David Goodmanson
le 21 Jan 2020
Modifié(e) : David Goodmanson
le 21 Jan 2020
Hi Erin,
tspan = [0 4];
y0 = 3;
[t,y] = ode45(@(t,y) t^2-(t*y), tspan, y0);
plot(t,y); grid on
The span of t here duplicates the right hand half of your sample plot. Once you use grid on, as you almost always should, you will probably like the comparison. Negative x would be a separate calculation.
Walter Roberson
le 24 Jan 2020
Your "correct graph" is a vector field or quiver plot. Each point on a grid needs a direction and a length, or a pair of lengths that together are interpreted as a vector. There is nothing in the code you showed us that provides the information that would be needed for such a plot.
Réponse acceptée
Plus de réponses (1)
In R2023b, a completely new interface for working with ODEs was released. The old ways are all still in MATLAB, of course, but the new way is much easier to use in my opinion. Here's how you'd solve your problem using the new interface
Details in my blog post The new solution framework for Ordinary Differential Equations (ODEs) in MATLAB R2023b » The MATLAB Blog - MATLAB & Simulink (mathworks.com)
F = ode(ODEFcn=@(t,y) t^2-t*y,InitialTime=0,InitialValue=3); % Set up your problem
sol = solve(F,-4,4); % solve it over [-4,4]
plot(sol.Time,sol.Solution) % Plot the solution
grid on
Catégories
En savoir plus sur Mathematics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



