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:
Beginner trying to solve a system of differential equations

Subject: Beginner trying to solve a system of differential equations

From: Nicholas

Date: 5 Nov, 2010 17:58:05

Message: 1 of 11

I am new to Matlab and have been trying to learn how to set up a system of differential equations. I found the following example from Matlab's help files:

function dy = rigid(t,y)
dy = zeros(3,1);
dy(1) = y(2) * y(3);
dy(2) = -y(1) * y(3);
dy(3) = -0.51 * y(1) * y(2);
options = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4 1e-5]);
[T,Y] = ode45(@rigid,[0 12],[0 1 1],options);
plot(T,Y(:,1),'-',T,Y(:,2),'-.',T,Y(:,3),'.')

This code is used to solve the following system:

y'1 = y2*y3 y1(0) = 0
y'2 = -y1*y3 y2(0) = 1
y'3 = -0.51*y1*y2 y3(0) = 1

From what I understand so far, you have to save the following code as an m file in order for it to solve it. Then you run the function rigid in the command window. When I run the function in the command window, I get the following error:

??? Input argument "y" is undefined.

Error in ==> rigid at 3
dy(1) = y(2) * y(3);
 
Now somebody with quite a bit of Matlab experience is probably thinking, "This guy does not know apples from oranges," but remember, I am new at this. If you know what I am doing wrong then please fix the code for me and give me an easy to understand explanation. I am still taking my baby steps.

Any help will be very much appreciated. Thanks.

Subject: Beginner trying to solve a system of differential equations

From: Sean

Date: 5 Nov, 2010 18:07:04

Message: 2 of 11

"Nicholas " <nrh401@jaguar1.usouthal.edu> wrote in message <ib1gjd$e6r$1@fred.mathworks.com>...
> I am new to Matlab and have been trying to learn how to set up a system of differential equations. I found the following example from Matlab's help files:
>
> function dy = rigid(t,y)
> dy = zeros(3,1);
> dy(1) = y(2) * y(3);
> dy(2) = -y(1) * y(3);
> dy(3) = -0.51 * y(1) * y(2);
> options = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4 1e-5]);
> [T,Y] = ode45(@rigid,[0 12],[0 1 1],options);
> plot(T,Y(:,1),'-',T,Y(:,2),'-.',T,Y(:,3),'.')
>
> This code is used to solve the following system:
>
> y'1 = y2*y3 y1(0) = 0
> y'2 = -y1*y3 y2(0) = 1
> y'3 = -0.51*y1*y2 y3(0) = 1
>
> From what I understand so far, you have to save the following code as an m file in order for it to solve it. Then you run the function rigid in the command window. When I run the function in the command window, I get the following error:
>
> ??? Input argument "y" is undefined.
>
> Error in ==> rigid at 3
> dy(1) = y(2) * y(3);
>
> Now somebody with quite a bit of Matlab experience is probably thinking, "This guy does not know apples from oranges," but remember, I am new at this. If you know what I am doing wrong then please fix the code for me and give me an easy to understand explanation. I am still taking my baby steps.
>
> Any help will be very much appreciated. Thanks.


When you call rigid, you have to feed it input arguments, you can't just play it with the green play button.

something like this:
T = 1:10;
y = exp(T);

dy = rigid(T,y)

Now when the function rigid is called, y won't be undefined like it is in your error message.

Subject: Beginner trying to solve a system of differential equations

From: Steven_Lord

Date: 5 Nov, 2010 20:10:14

Message: 3 of 11



"Nicholas " <nrh401@jaguar1.usouthal.edu> wrote in message
news:ib1gjd$e6r$1@fred.mathworks.com...
> I am new to Matlab and have been trying to learn how to set up a system of
> differential equations. I found the following example from Matlab's help
> files:
>
> function dy = rigid(t,y)
> dy = zeros(3,1);
> dy(1) = y(2) * y(3);
> dy(2) = -y(1) * y(3);
> dy(3) = -0.51 * y(1) * y(2);
> options = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4 1e-5]);
> [T,Y] = ode45(@rigid,[0 12],[0 1 1],options);
> plot(T,Y(:,1),'-',T,Y(:,2),'-.',T,Y(:,3),'.')

DO NOT call any of the ODE solvers inside the functions on which they are to
operate!

Move the last three lines of your code out of the rigid function and execute
them at the command line or in a separate script or function file.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Beginner trying to solve a system of differential equations

From: Nicholas

Date: 5 Nov, 2010 23:21:06

Message: 4 of 11

"Steven_Lord" <slord@mathworks.com> wrote in message <ib1ob6$f2e$1@fred.mathworks.com>...
>
>
> "Nicholas " <nrh401@jaguar1.usouthal.edu> wrote in message
> news:ib1gjd$e6r$1@fred.mathworks.com...
> > I am new to Matlab and have been trying to learn how to set up a system of
> > differential equations. I found the following example from Matlab's help
> > files:
> >
> > function dy = rigid(t,y)
> > dy = zeros(3,1);
> > dy(1) = y(2) * y(3);
> > dy(2) = -y(1) * y(3);
> > dy(3) = -0.51 * y(1) * y(2);
> > options = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4 1e-5]);
> > [T,Y] = ode45(@rigid,[0 12],[0 1 1],options);
> > plot(T,Y(:,1),'-',T,Y(:,2),'-.',T,Y(:,3),'.')
>
> DO NOT call any of the ODE solvers inside the functions on which they are to
> operate!
>
> Move the last three lines of your code out of the rigid function and execute
> them at the command line or in a separate script or function file.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
Thanks a lot. That did the trick, but I have another question. How do you view the numerical value of any of the y-functions at a particular t-value such as y1(2) = 1.

Subject: Beginner trying to solve a system of differential equations

From: Steven_Lord

Date: 6 Nov, 2010 23:01:04

Message: 5 of 11



"Nicholas " <nrh401@jaguar1.usouthal.edu> wrote in message
news:ib23h2$b99$1@fred.mathworks.com...
> "Steven_Lord" <slord@mathworks.com> wrote in message
> <ib1ob6$f2e$1@fred.mathworks.com>...

*snip*

> Thanks a lot. That did the trick, but I have another question. How do you
> view the numerical value of any of the y-functions at a particular t-value
> such as y1(2) = 1.

The easiest way is to call the ODE solver not with two outputs but with
one -- the syntax involving SOL in the help. Then you can use DEVAL to
evaluate the solution at desired times.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Beginner trying to solve a system of differential equations

From: Nicholas

Date: 7 Nov, 2010 03:41:03

Message: 6 of 11

"Steven_Lord" <slord@mathworks.com> wrote in message <ib4mnh$ae1$1@fred.mathworks.com>...
>
>
> "Nicholas " <nrh401@jaguar1.usouthal.edu> wrote in message
> news:ib23h2$b99$1@fred.mathworks.com...
> > "Steven_Lord" <slord@mathworks.com> wrote in message
> > <ib1ob6$f2e$1@fred.mathworks.com>...
>
> *snip*
>
> > Thanks a lot. That did the trick, but I have another question. How do you
> > view the numerical value of any of the y-functions at a particular t-value
> > such as y1(2) = 1.
>
> The easiest way is to call the ODE solver not with two outputs but with
> one -- the syntax involving SOL in the help. Then you can use DEVAL to
> evaluate the solution at desired times.
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
Thank you very much. That did it for me. The next thing that I am going to work on is how to solve second order differential equations. Once I figure this out, I can start to work on my project. If you can give me a quick little example for this in this post then it will be very much appreciated. If not then that is okay, I will just try to cross that bridge when I come to it. Thanks for your help so far Steven Lord.

Subject: Beginner trying to solve a system of differential equations

From: Brian Borchers

Date: 7 Nov, 2010 04:27:22

Message: 7 of 11

> Thank you very much. That did it for me. The next thing that I am going to work on is how to solve second order differential equations. Once I figure this out, I can start to work on my project. If you can give me a quick little example for this in this post then it will be very much appreciated. If not then that is okay, I will just try to cross that bridge when I come to it.

You should learn a standard trick here. Any nth order differential
equation initial value problem can be be turned into a an initial
value problem for a system of n first order differential equations.
The trick is to connect together n different functions, each of which
is the derivative of the previous function and then write the original
higher order equation in terms of these n function.

e.g. Suppose that you have

y''(x)=f(x,y,y'), y(0)=y0, y'(0)=y0'

Then let

u=y(x)
v=y'(x)

The initial value problem becomes

v'(x)=f(x,u,v)
u'(x)=v(x)

u(0)=y0
v(0)=y0'

The trick is easily extended to initial value problems with nth order
ODE's.

Subject: Beginner trying to solve a system of differential equations

From: Nicholas

Date: 8 Nov, 2010 00:23:04

Message: 8 of 11

Brian Borchers <borchers.brian@gmail.com> wrote in message <34409d0c-72b3-4369-9588-aee3e5ab0504@r29g2000yqj.googlegroups.com>...
> > Thank you very much. That did it for me. The next thing that I am going to work on is how to solve second order differential equations. Once I figure this out, I can start to work on my project. If you can give me a quick little example for this in this post then it will be very much appreciated. If not then that is okay, I will just try to cross that bridge when I come to it.
>
> You should learn a standard trick here. Any nth order differential
> equation initial value problem can be be turned into a an initial
> value problem for a system of n first order differential equations.
> The trick is to connect together n different functions, each of which
> is the derivative of the previous function and then write the original
> higher order equation in terms of these n function.
>
> e.g. Suppose that you have
>
> y''(x)=f(x,y,y'), y(0)=y0, y'(0)=y0'
>
> Then let
>
> u=y(x)
> v=y'(x)
>
> The initial value problem becomes
>
> v'(x)=f(x,u,v)
> u'(x)=v(x)
>
> u(0)=y0
> v(0)=y0'
>
> The trick is easily extended to initial value problems with nth order
> ODE's.
Thank you very much.

Subject: Beginner trying to solve a system of differential equations

From: Nicholas

Date: 8 Nov, 2010 17:12:04

Message: 9 of 11

If I have the following m file in Matlab:

function dy = rigid(t,y)
dy = zeros(3,1);
dy(1) = Coeff1 * y(2) * y(3);
dy(2) = -y(1) * Coeff2 * y(3);
dy(3) = -0.51 * y(1) * y(2);

How do I fix this m file so that I can input a value for Coeff1 and Coeff2 from the command window or from another m file that would call this function, and how do I call it from the command window after it is fixed?

Subject: Beginner trying to solve a system of differential equations

From: Steven_Lord

Date: 8 Nov, 2010 19:01:28

Message: 10 of 11



"Nicholas " <nrh401@jaguar1.usouthal.edu> wrote in message
news:ib9b14$gi9$1@fred.mathworks.com...
> If I have the following m file in Matlab:
>
> function dy = rigid(t,y)
> dy = zeros(3,1);
> dy(1) = Coeff1 * y(2) * y(3);
> dy(2) = -y(1) * Coeff2 * y(3);
> dy(3) = -0.51 * y(1) * y(2);
>
> How do I fix this m file so that I can input a value for Coeff1 and Coeff2
> from the command window or from another m file that would call this
> function, and how do I call it from the command window after it is fixed?

Have your function accept an additional parameter. Call the ODE solver with
an anonymous function that accepts two inputs: t and y, and calls your
function with three inputs: t, y, and your coefficient values.

function dy = rigid(t,y, Coeff1)
% snip the remainder of rigid.m

% To call it:
mu1 = 2;
sol = ode45(@(t, y) rigid(t, y, mu1), ...

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Subject: Beginner trying to solve a system of differential equations

From: Nicholas

Date: 8 Nov, 2010 23:27:04

Message: 11 of 11

"Steven_Lord" <slord@mathworks.com> wrote in message <ib9he8$eql$1@fred.mathworks.com>...
>
>
> "Nicholas " <nrh401@jaguar1.usouthal.edu> wrote in message
> news:ib9b14$gi9$1@fred.mathworks.com...
> > If I have the following m file in Matlab:
> >
> > function dy = rigid(t,y)
> > dy = zeros(3,1);
> > dy(1) = Coeff1 * y(2) * y(3);
> > dy(2) = -y(1) * Coeff2 * y(3);
> > dy(3) = -0.51 * y(1) * y(2);
> >
> > How do I fix this m file so that I can input a value for Coeff1 and Coeff2
> > from the command window or from another m file that would call this
> > function, and how do I call it from the command window after it is fixed?
>
> Have your function accept an additional parameter. Call the ODE solver with
> an anonymous function that accepts two inputs: t and y, and calls your
> function with three inputs: t, y, and your coefficient values.
>
> function dy = rigid(t,y, Coeff1)
> % snip the remainder of rigid.m
>
> % To call it:
> mu1 = 2;
> sol = ode45(@(t, y) rigid(t, y, mu1), ...
>
> --
> Steve Lord
> slord@mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
> To contact Technical Support use the Contact Us link on
> http://www.mathworks.com
Thank you very much.

Tags for 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