Plotting first-order differential equation with initial condition

조회 수: 254 (최근 30일)
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
David Goodmanson
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
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Guru Mohanty
Guru Mohanty 2020년 1월 24일
Hi, I understand you are trying to plot the differential equation. The issue while plotting with ode45 is it tries to solve it numerically. The solver imposes the initial conditions given by y0 at the initial time tspan(1), then integrates from tspan(1) to tspan(end). But in your scenario initial condition is at t = 0 and tspan(1) = -4. If you plot the ODE in the interval [0, 4] the ode45 will give correct result. However, you can plot the solution of differential equation using Symbolic toolbox and dsolve function. You may mention the appropriate range in fplot while plotting the solution. Here is the code for it.
clc;
clear all;
syms y(t)
eqn = diff(y,1)+ t*y ==t^2;
cond = y(0)==3;
sol = dsolve(eqn,cond);
fplot(sol,[-4,4]);
grid on;
case_ode.jpg
  댓글 수: 2
Robin James
Robin James 2020년 12월 6일
편집: Robin James 2020년 12월 6일
I am trying to do something similar: See code below
clc
close all
clear all
Re = 1522.554;
Pr = 0.7;
K = 54.13e-3;
D = 15e-3;
Nu = 2 + ((((0.4*(Re^(0.5)))+((0.06*(Re^(2/3)))))*(Pr^(0.4)))*((1)^(1/4)))
hbar = (Nu*K)/D
As = pi*(D^2);
Tinf = 273.15+900;
Tsur = 273.15+600;
epsilon = 0.5;
sigma = 5.6703e-8; % (W/m2K4) S-B constant
syms x
eqn = ((epsilon*sigma*((Tsur^4)-(x^4)))-(hbar*(x-Tinf)))*As == 0;
T = vpasolve(eqn)
rho = 0.4743;
v = (4/3)*pi*((D/2)^3);
c = 1084;
syms T(t)
eqn2 = diff(T,t) == (-As/(rho*v*c))*((hbar*(T - Tinf))+(epsilon*sigma*(((T)^4)-(Tsur^4))))
cond = T(0) == 273.15+25;
TSol(t) = dsolve(eqn2,cond);
fplot(TSol,[0,15000]);
grid on
But I am getting error:
Error using fplot (line 120)
Input must be a function handle or symbolic function.
Error in problem4 (line 48)
fplot(TSol,[0,15000]);
Walter Roberson
Walter Roberson 2020년 12월 6일
The equation generated in eqn2 is of the form A+B*T^4+C*T==diff(T) but MATLAB is not able to resolve that.
Mathematica does appear to be able to resolve equations of that general form: https://www.wolframalpha.com/input/?i=A+%2B+B*T%5Bt%5D%5E4+%2B+C*T%5Bt%5D+%3DT%27%5Bt%5D

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Mike Croucher
Mike Croucher 2023년 10월 3일
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
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

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by