Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Matlab m file writing help

Subject: Matlab m file writing help

From: Lord Luscious

Date: 15 Oct, 2009 23:48:56

Message: 1 of 3

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?

Subject: Matlab m file writing help

From: Steven Lord

Date: 16 Oct, 2009 14:25:54

Message: 2 of 3


"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.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: Matlab m file writing help

From: Lord Luscious

Date: 28 Oct, 2009 01:22:54

Message: 3 of 3

Thank you very much for your help.

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us