# Plotting first-order differential equation with initial condition

514 views (last 30 days)

Show older comments

Erin Manogaran
on 21 Jan 2020

Answered: Mike Croucher
on 3 Oct 2023

##### 4 Comments

David Goodmanson
on 21 Jan 2020

Edited: David Goodmanson
on 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
on 24 Jan 2020

### Accepted Answer

Guru Mohanty
on 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 Comments

Robin James
on 6 Dec 2020

Edited: Robin James
on 6 Dec 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
on 6 Dec 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

### More Answers (1)

Mike Croucher
on 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 Comments

### See Also

### Categories

### Community Treasure Hunt

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

Start Hunting!