"Lord Luscious" <sillywilly082000@yahoo.com> wrote in message
news:829635843.66493.1255650566391.JavaMail.root@gallium.mathforum.org...
> I'm trying to write a function file for the ordinary differential
> equation:
> dy/dx+y*tan(x)=sin(2x) y(0)=1
> For x=0 to x=2? using ODE45
>
> this is my m file so far:
>
> function dydx = cexamn_one(x,y)
> dy=sin(2x)ytan(x);
> [x,y] = ode45(@ecexamn_one,[0,6.283],1);
> plot(x,y)
>
> In matlab, it won't work, saying x isn't defined.
> What do I type in my m file to fix this?
I assume that your call to ODE45 has a typo and that the first input is
intended to be @cexamn_one; if that is the case, that is not the correct way
to call ODE45.
NEVER call an ODE solver, or an Optimization Toolbox function, or any
"function function" with a handle to the function you're trying to
solve/optimize as input. You'll encounter this type of error, or you'll
receive an error about the RecursionLimit, or (if you've changed the
RecursionLimit to too large a value) you could even crash MATLAB.
If you want all your code to be in one function file, write the ODE function
(that you pass into ODE45) as a subfunction and call ODE45 from the main
function in that file. I made a few tweaks to the code, mostly to fix the
implicit multiplications you used (2x is not interpreted as 2*x, it's
interpreted as an invalid variable or function name. Similarly, ytan is not
y*tan but is a reference to a variable or function named ytan.)
% begin mymainfunction.m
function mymainfunction
% Fixed typo and used 2*pi instead of your approximation
[x,y] = ode45(@cexamn_one,[0 2*pi],1);
plot(x,y)
function dydx = cexamn_one(x,y)
dydx=sin(2*x)y*tan(x); % Added the * between 2 and x and y and tan
% end mymainfunction.m

Steve Lord
slord@mathworks.com
comp.softsys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
