Plotting first-order differential equation with initial condition
显示 更早的评论
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 个评论
KSSV
2020-1-21
Read about ODE45 to get the solution and read about plot to get the graph.
Erin Manogaran
2020-1-21
David Goodmanson
2020-1-21
编辑:David Goodmanson
2020-1-21
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
2020-1-24
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.
采纳的回答
更多回答(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
类别
在 帮助中心 和 File Exchange 中查找有关 Mathematics 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



