Solving Diff. Eq. with MATLAB ode45

  • MATLAB
  • Thread starter klinklindeman
  • Start date
  • Tags
    Matlab Ode45
In summary, to solve a differential equation using ode45 in MATLAB, you need to define the equation as a function and use the ode45 function with the appropriate syntax. ode45 has several advantages, including being a built-in function, using an adaptive time step, and being able to handle a wide range of differential equations. To plot the solution from ode45, you can use the plot function. ode45 can also handle systems of differential equations. To improve the accuracy of the solution, you can adjust the tolerance level, increase the number of output points, or try using a different solver.
  • #1
klinklindeman
2
0
Hello,

I am trying to write a program on MATLAB using ode45 to solve the following equations:

S'=-bSZ-sS
Z'=bSZ+gR-aSZ
R'=sS+aSZ-gR

These are all derivatives with respect to t, differential equations.

a, b, a and g are all constants.

I realize I need to have two m files, one which defines the functions, and one which calls that and uses ode45. The code for my first m file is:


function Yout = program1(S,Z,R);

global beta gamma alpha sigma;

% set the parmeter values
tend = 10;
S0 = 500;
I0 = 1;
b=0.0095;
g=0.0001;
a= 0.005;
s=0.0001;

Sdot=(-b)*S*Z-(gg*S);

Zdot=(b*S*Z)+(g*R)-(a*S*Z);

Rdot=(s*S)+(a*S*Z)-(g*R);

udot = [Sdot; Zdot; Rdot];

I'm not sure if that is suitable, and I also need help writing the second m file. I need to plot Z and R on the same plot, vs t. I am really stuck, I have been working for hours, looking at examples, trying different combinations. MATLAB is most definitely not my strong point.

Thanks for the help!
 
Physics news on Phys.org
  • #2
</code>The first step for solving the equations is to define the functions that describe their derivatives. This can be done in a separate MATLAB script, as you have done above.Next, you need to call the ode45 function. To do this, you will need to create a second MATLAB script. This script should contain the following code:% Define constantsbeta = 0.0095;gamma = 0.0001;alpha = 0.005;sigma = 0.0001;% Set Initial Conditionst0 = 0; S0 = 500; Z0 = 1;R0 = 0;% Solve the differential equations[t,y] = ode45(@program1,[t0,T], [S0,Z0,R0]);% Extract S, Z, and R from yS = y(:,1);Z = y(:,2);R = y(:,3);% Plot the resultsplot(t,Z,t,R);xlabel('Time (t)');ylabel('Population');title('Variation of Z and R with Time');legend('Z','R');In the above code, you call the ode45 function to solve the differential equations with the initial conditions given and the parameters set as global variables. The output of ode45 is then used to plot the resulting populations of Z and R over time.I hope this helps!
 

1. How do I solve a differential equation using MATLAB's ode45 function?

To solve a differential equation using ode45 in MATLAB, you need to first define the differential equation as a function and then use the ode45 function to solve it. The syntax for ode45 is as follows: [t,y] = ode45(@func, tspan, y0), where @func is the name of the function that defines the differential equation, tspan is the time interval for the solution, and y0 is the initial condition. The output t is a vector of time values and y is a matrix of the corresponding solution values.

2. What are the advantages of using ode45 for solving differential equations in MATLAB?

One advantage of using ode45 is that it is a built-in function in MATLAB, which means you do not need to write your own code for solving differential equations. ode45 also uses an adaptive time step, which helps to improve the accuracy and efficiency of the solution. Additionally, ode45 can handle a wide range of differential equations, including stiff and non-stiff equations.

3. How do I plot the solution obtained from ode45 in MATLAB?

After solving the differential equation using ode45, you can plot the solution by using the plot function in MATLAB. The syntax for the plot function is as follows: plot(t,y), where t is the vector of time values and y is the matrix of solution values obtained from ode45. This will create a plot of the solution over the specified time interval.

4. Can ode45 handle systems of differential equations?

Yes, ode45 can handle systems of differential equations. To solve a system of differential equations, the function defined for ode45 should take in vectors as input and output. The initial condition and solution matrix will also be vectors in this case.

5. How can I improve the accuracy of the solution obtained from ode45?

There are a few ways to improve the accuracy of the solution obtained from ode45. One way is to decrease the tolerance level by setting the 'RelTol' and 'AbsTol' options in the ode45 function. Another way is to increase the number of output points by specifying a finer time interval in the tspan argument. You can also try using a different solver, such as ode23 or ode113, to see if it produces a more accurate solution.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
986
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
5
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
Back
Top