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!



