Plotting first-order differential equation with initial condition
251 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Erin Manogaran
le 21 Jan 2020
Réponse apportée : Mike Croucher
le 3 Oct 2023
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
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
Guru Mohanty
le 24 Jan 2020
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;
2 commentaires
Robin James
le 6 Déc 2020
Modifié(e) : Robin James
le 6 Déc 2020
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
le 6 Déc 2020
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
Plus de réponses (1)
Mike Croucher
le 3 Oct 2023
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
0 commentaires
Voir également
Catégories
En savoir plus sur Calculus dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!