A simple first order ode evaluated at specific points

| categories: odes | View Comments

A simple first order ode evaluated at specific points

A simple first order ode evaluated at specific points

In Post 648 , we integrated an ODE over a specific time span. Sometimes it is desirable to get the solution at specific points, e.g. at t = [0 0.2 0.4 0.8]; This could be desirable to compare with experimental measurements at those time points. This post demonstrates how to do that.

$$\frac{dy}{dt} = y(t)$$

The initial condition is y(0) = 1.

function ode_01_1
close all; clc;
y0 = 1; % initial condition y(t=t0)
tspan = [0 0.2 0.4 0.8]; % the solution will only be evaluated at these
                         % points.
[t,y] = ode45(@myode,tspan,y0);

plot(t,y,'ko-')
xlabel('time')
ylabel('y(t)')
'end'
ans =

end

function dydt = myode(t,y)
% differential equation to solve
dydt = y;

% categories: ODEs
% tags: math
Read and Post Comments

Numerical solution to a simple ode

| categories: odes | View Comments

Numerical solution to a simple ode:

Numerical solution to a simple ode:

Integrate this ordinary differential equation (ode)

Contents

$$\frac{dy}{dt} = y(t)$$

over the time span of 0 to 2. The initial condition is y(0) = 1.

to solve this equation, you need to create a function or function handle of the form: dydt = f(t,y) and then use one of the odesolvers, e.g. ode45.

function main

problem setup

y0 = 1; % initial condition y(t=t0)
t0 = 0; tend=2;
tspan = [t0 tend]; % span from t=0 to t=2
[t,y] = ode45(@myode,tspan,y0); % here is where you get the solution

plot(t,y)
xlabel('time')
ylabel('y(t)')

analytical solution

Hopefully you recognize the solution to this equation is $y(t)=e^t$. Let's plot that on the same graph. We will use a dashed red line

hold on
plot(t,exp(t),'r--')
legend('numerical solution','analytical solution')
% these are clearly the same solution.
'end'
ans =

end

function dydt = myode(t,y)

differential equation to solve $\frac{dy}{dt} = y(t)$

dydt = y;

% categories: ODEs
% tags: math
Read and Post Comments

« Previous Page