0% found this document useful (0 votes)
588 views

Ode23 Example

The document provides instructions to solve a first order differential equation numerically using Matlab's ode23 and ode45 functions. It involves: 1) Defining the differential equation in a function file fun1.m; 2) Using ode23 and ode45 to solve the initial value problem and store the results in vectors; 3) Plotting the numerical solutions on the same graph for comparison; 4) Reporting the solution value for a given time point.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
588 views

Ode23 Example

The document provides instructions to solve a first order differential equation numerically using Matlab's ode23 and ode45 functions. It involves: 1) Defining the differential equation in a function file fun1.m; 2) Using ode23 and ode45 to solve the initial value problem and store the results in vectors; 3) Plotting the numerical solutions on the same graph for comparison; 4) Reporting the solution value for a given time point.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Q1: Use ode23 and ode45 to solve the initial value problem for a first order differential equation:

y' =

ty 2 y2

, y (0) = 1, t [0, 5]

Step1: First create a MatLab function and name it fun1.m.


function f=fun1(t,y) f=-t*y/sqrt(2-y^2);

Save the files as fun1.m

Step2: Test the function by entering in the command window fun1(1,1). The result should be

This shows that the function is working fine. Step 3:

Now use MatLab functions ode23 and ode45 to solve the initial value problem numerically and then plot the numerical solutions y, respectively. In the MatLab window, type in the following commands line by line.
>> [tv1 f1]=ode23('fun1',[0 5],1); % Generates a set of points and stores them in vectors tv1 and f1 >> [tv2 f2]=ode45('fun1',[0 5],1); % Generates a set of points and stores them in vectors tv2 and f2 >> plot(tv1,f1,'-.',tv2,f2,'--') % plots two graphs with coordinate sets (tv1,f1) and (tv2,f2) >> title('y''=-ty/sqrt(2-y^2), y(0)=1, t in [0, 5]') % Provide a title to graph >> grid % Provide a grid >> axis([0 5 0 1]) % set the x and y axis limits to (0,5) and (0,1)

Note: Anything you enter after % is considered as comment line. Step 4: Read a particular solution for say t =2, the value of y and report the result.

There are two plots in the same graph, hardly distinguisahble since the results by ode23 and ode45 functions are close to each other.

You might also like