Professional Documents
Culture Documents
st
Example Problem
Use ode23 and ode45 to solve the initial value problem for 1 st order ODE
Governing equation
Initial
and
let
condition: y(0)=1
K=4
Divide
Solving Numerically
We will use the most common for this simple problem: ode45
2.
3.
[t,y] = solver(odefun,tspan,y0)
2.
3.
Create
function fout=fun1(t,y)
%this function accepts two inputs, calculates the
equation and returns the results into fout
K=4; %given
fout=-t*y/(K*sqrt(2-y^2)); %notice fout match
end
Call
the function file, fun1, from your main m-file,
mainm.m
mainm2.m
clear all, close all, clc; K=4; %given
f=@(t,y) -t*y/(K*sqrt(2-y^2)); %create an anonymous fcn to store RHS of ODE
timerange=[0,5]; % initialy=1; %
clear all, close all, clc; %This program compares ode45 & ode23 solvers
K=4;
f=@(t,y) -t*y/(K*sqrt(2-y^2)); %create an anonymous fcn to store RHS of ODE
timerange=[0,5]; %t?[0,5]
initialy=1; % initial conditions
[t1,y1]=ode45(f,timerange,initialy); %notice absence of @sign
subplot(2,1,1) %ode45 plot
plot(t1,y1,'r') %graphical view of solution
title('Solving dy/dt= -ty/K(2-y^2)^1^/^2 using ode45')
xlabel('t values from set'); ylabel('y values for each t'); legend('ode45')
subplot(2,1,2) %ode23 plot
[t2,y2]=ode23(f,timerange,initialy); %notice absence of @sign
plot(t2,y2) %graphical view of solution
title('Solving dy/dt= -ty/K(2-y^2)^1^/^2 using ode23')
xlabel('t values from set'); ylabel('y values for each t'); legend('ode23')
Solutions
What is y(3)?