plot first order de in matlab

To plot a first order differential equation in MATLAB, you can use the ode45 function.

In the code below, we will plot the first order differential equation: dy/dt = -2*y

main.m
% Define the function handle for the differential equation
odefun = @(t,y) -2*y;

% Define the initial conditions
y0 = 1;

% Define the time interval for the plot
tspan = [0 10];

% Solve the differential equation using the ode45 function
[t,y] = ode45(odefun,tspan,y0);

% Plot the results
plot(t,y)
xlabel('Time')
ylabel('y')
title('Solution to dy/dt = -2*y')
362 chars
18 lines

This code will plot the solution to the first order differential equation dy/dt = -2*y. The ode45 function solves the differential equation for the time interval specified by tspan, with the initial condition y0. The resulting solution is stored in the vectors t and y, which are plotted using the plot function. The xlabel, ylabel, and title functions are used to label the plot.

gistlibby LogSnag