Register to reply

Matlab ODE45 Help?

by Mrs_ChemE
Tags: matlab, ode45
Share this thread:
Mrs_ChemE
#1
Oct27-10, 06:01 PM
P: 3
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
Phys.Org News Partner Science news on Phys.org
Scientists develop 'electronic nose' for rapid detection of C. diff infection
Why plants in the office make us more productive
Tesla Motors dealing as states play factory poker
matematikawan
#2
Nov6-10, 11:10 AM
P: 333
Not sure whats 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);
Mrs_ChemE
#3
Nov6-10, 02:13 PM
P: 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.

matematikawan
#4
Nov6-10, 10:36 PM
P: 333
Matlab ODE45 Help?

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.
swraman
#5
Nov11-10, 12:45 AM
P: 145
Quote Quote by Mrs_ChemE View Post
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.

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.


Register to reply

Related Discussions
Need help for ODE45 matlab Math & Science Software 3
Ode45 matlab Introductory Physics Homework 1
Ode45 help in MATLAB Math & Science Software 0
Matlab Help:Stop ode45 with OutpuFcn Math & Science Software 0
MatLab help using ode23/ode45 Math & Science Software 2