ODE45 Help for Matlab: Fix Your Code Now

  • MATLAB
  • Thread starter Mrs_ChemE
  • Start date
  • Tags
    Matlab Ode45
In summary: You can also simply type "ode45" at the MATLAB command line.ThanksNot sure what's wrong with it. Probably you need a separate m-file for the function. If you need to include the function in your script try using function handlefun =@(t,y) cos(y) - (sin (t)*y);[t,y] = ode45 (fun,[0,1],0);Thanks Matematikawan.
  • #1
Mrs_ChemE
3
0
Matlab ODE45 Help??

function dy = prob_52 (t,y)
dy = cos(y) - (sin (t)*y);

[t,y] = ode45 (@prob_52,[0,1],0)
plot (t,y)
xlabel ('Time')
ylabel ('Function Value')

I was told my code is incorrect. Does anyone have suggestions on where I'm going wrong?

Thanks
 
Physics news on Phys.org
  • #2


Not sure what's wrong with it. Probably you need a separate m-file for the function. If you need to include the function in your script try using function handle
fun =@(t,y) cos(y) - (sin (t)*y);

[t,y] = ode45 (fun,[0,1],0);
 
  • #3


Thanks Matematikawan.

Right now, I have a separate file named "Prob_52" that has the function and then a file with:
[t,y] = ode45 (@prob_52,[0,1],0)
plot (t,y)
xlabel ('Time')
ylabel ('Function Value')

My professor is unable to run it, which makes it a fail. I'm wondering if this is because his computer can't "see" the Prob_52 file?

I will change it to the way you have it. Do you know how I can test it before I submit it? This is my last shot. If this one doesn't work, I fail my Chem E course. :(

When I save my files, etc, it's fine. But I *assume* that's because I have it "all" on my PC when I'm working.
 
  • #4


I'm not an expert in matlab. Just able to run basic commands.

I think you need to submit two m-files to your professor. One is your function m-file and the other is your script that contain ode45. If that doesn't works also, please check that your prof. MATLAB is not of lower version than your matlab.

If you need to submit just one file then use the function handle as in my previous post.
 
  • #5


Mrs_ChemE said:
Thanks Matematikawan.

Right now, I have a separate file named "Prob_52" that has the function and then a file with:
[t,y] = ode45 (@prob_52,[0,1],0)
plot (t,y)
xlabel ('Time')
ylabel ('Function Value')

My professor is unable to run it, which makes it a fail. I'm wondering if this is because his computer can't "see" the Prob_52 file?

That may be the case. MATLAB has an environment variable called the Path, which is the list of places it looks for files you tel it to run. The first place it looks is the Current Directory, which you set in the MATLAB gui or at the command line. If your prob_52.m file is not in MATLAB's current directory when you/your prof runs the script, it will not find the file.

Since your problem is only a first order equation, it is possible to handle it is to use an anonymous function, as matematikawan suggested.

Code:
prob_52 = @(t,y)cos(y) - (sin (t)*y);
[t,y] = ode45 (prob_52,[0,1],0)
plot (t,y)
xlabel ('Time')
ylabel ('Function Value')
Note here in the call to ODE45, you don't use the @ symbol, because prob_52 is of class function_handle, which is what ODE45 takes in.
 

1. What is ODE45 in Matlab?

ODE45 is a numerical integration function in Matlab that is used to solve ordinary differential equations (ODEs). It uses an adaptive Runge-Kutta method to approximate the solution of the ODEs over a specified time interval.

2. How do I use ODE45 in Matlab?

To use ODE45, you need to define the ODEs you want to solve as a function in Matlab. Then, you can call the ODE45 function with the appropriate inputs, including the initial conditions, time interval, and the function that defines the ODEs. ODE45 will return the solution to the ODEs as an array.

3. What are the common errors when using ODE45 in Matlab?

Some common errors when using ODE45 in Matlab include incorrect input arguments, such as using a non-numerical initial condition, or not defining the ODEs correctly in the function. Another common error is when the solution diverges, which may be caused by a poorly chosen time interval or incorrect ODEs.

4. How can I fix errors when using ODE45 in Matlab?

To fix errors when using ODE45, you should carefully check your input arguments and make sure they are in the correct format. You should also double-check your ODE function to ensure it accurately represents the ODEs you want to solve. If the solution diverges, you may need to adjust the time interval or refine your ODE function.

5. Can ODE45 be used for all types of ODEs?

No, ODE45 is designed specifically for non-stiff ODEs, which are equations that do not have rapidly changing solutions. For stiff ODEs, a different solver, such as ODE15s, may be more appropriate. It is important to understand the properties of your ODEs before choosing a solver in Matlab.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
996
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
825
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top