MATLAB: Plotting first-order differential equation with initial condition

differential equationsplotting

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.

Best Answer

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
Related Question