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:
quiry

Subject: quiry

From: Anna Qureshi

Date: 25 Jun, 2011 18:58:04

Message: 1 of 8

Hi,
following is the program code that i am trying to run, however it seems that the variables in the for loop are not recognized. If i use the command disp(k1), there is an error undefined. please help me fix this.

%4th ORDER RUNGE KUTTA METHOD
function y = rk(x,y,k1,k2,k3,k4,h)
clear;
%Given that : f(x,y) = dy/dx = -y^2
%Manually solving this question yielded y = 0.8029
%Where y(x0) = 1

% initialize
x(1) = 0;
y(1) = 1;

f= -square(y);
h=1;
n = 10;

% iterate
for i = 1:-n;
    k1 = f(x(i), y(i));
k2 = f(x(i) + h/2, y(i) + h/2*k1);
k3 = f(x(i) + h/2, y(i) + h/2*k2);
k4 = f(x(i) + h, y(i) + h*k3);
y(i+1) = y(i) + h/6*(k1 + 2*k2 + 2*k3 + k4);
x(i+1) = x(i) + h;
end

Subject: quiry

From: Roger Stafford

Date: 25 Jun, 2011 21:41:05

Message: 2 of 8

"Anna Qureshi" wrote in message <iu5b3s$l37$1@newscl01ah.mathworks.com>...
> Hi,
> following is the program code that i am trying to run, however it seems that the variables in the for loop are not recognized. If i use the command disp(k1), there is an error undefined. please help me fix this.
>
> %4th ORDER RUNGE KUTTA METHOD
> function y = rk(x,y,k1,k2,k3,k4,h)
> clear;
> %Given that : f(x,y) = dy/dx = -y^2
> %Manually solving this question yielded y = 0.8029
> %Where y(x0) = 1
>
> % initialize
> x(1) = 0;
> y(1) = 1;
>
> f= -square(y);
> h=1;
> n = 10;
>
> % iterate
> for i = 1:-n;
> k1 = f(x(i), y(i));
> k2 = f(x(i) + h/2, y(i) + h/2*k1);
> k3 = f(x(i) + h/2, y(i) + h/2*k2);
> k4 = f(x(i) + h, y(i) + h*k3);
> y(i+1) = y(i) + h/6*(k1 + 2*k2 + 2*k3 + k4);
> x(i+1) = x(i) + h;
> end
- - - - - - - - - -
1. There is no function named 'square' in the basic matlab set. Unless it is in some toolbox that I am unaware of or unless you have defined it in an m-file of your own, you will get an error message saying it is undefined.

2. Your for loop is set to count up from 1, but its end number is -10. In that case the for loop is totally frustrated and will do nothing. In any event, you cannot use non-positive integers as indices for x and y. They must all be positive integers 1,2,3,4,etc.

Roger Stafford

Subject: quiry

From: Roger Stafford

Date: 25 Jun, 2011 21:53:04

Message: 3 of 8

"Roger Stafford" wrote in message <iu5klh$e4e$1@newscl01ah.mathworks.com>...
> "Anna Qureshi" wrote in message <iu5b3s$l37$1@newscl01ah.mathworks.com>...
> > f= -square(y);
> 1. There is no function named 'square' in the basic matlab set. Unless it is in some toolbox that I am unaware of or unless you have defined it in an m-file of your own, you will get an error message saying it is undefined.
 - - - - - - -
1a. Also that isn't the right way to define a function. Read up on anonymous functions in the documentation.

Roger Stafford

Subject: quiry

From: Nasser M. Abbasi

Date: 25 Jun, 2011 22:33:41

Message: 4 of 8

On 6/25/2011 11:58 AM, Anna Qureshi wrote:

> function y = rk(x,y,k1,k2,k3,k4,h)
......
> h=1;

hummm... why are you passing 'h' in and yet you
set it to '1' in the function?


--Nasser

Subject: quiry

From: sana kazmi

Date: 25 Jun, 2011 23:45:21

Message: 5 of 8

ok, so is f = -y^2 an incorrect way to define this function?
If h = 0.001, and N is positive, how can i get values from k1..k4

Subject: quiry

From: Roger Stafford

Date: 26 Jun, 2011 02:38:04

Message: 6 of 8

"sana kazmi" wrote in message <iu5ruh$1lu$1@newscl01ah.mathworks.com>...
> ok, so is f = -y^2 an incorrect way to define this function?
> If h = 0.001, and N is positive, how can i get values from k1..k4
- - - - - - - - - - -
  It is not valid matlab syntax. I have copied and pasted this section of matlab documentation concerning anonymous functions for your convenience. You would do well to read it carefully (along with many other parts of the documentation.)

"Constructing an Anonymous Function

Anonymous functions give you a quick means of creating simple functions without having to store your function to a file each time. You can construct an anonymous function either at the MATLAB command line or in any function or script.

The syntax for creating an anonymous function from an expression is

 fhandle = @(arglist) expr

Starting from the right of this syntax statement, the term expr represents the body of the function: the code that performs the main task your function is to accomplish. This consists of any single, valid MATLAB expression. Next is arglist, which is a comma-separated list of input arguments to be passed to the function. These two components are similar to the body and argument list components of any function.

Leading off the entire right side of this statement is an @ sign. The @ sign is the MATLAB operator that constructs a function handle. Creating a function handle for an anonymous function gives you a means of invoking the function. It is also useful when you want to pass your anonymous function in a call to some other function. The @ sign is a required part of an anonymous function definition."

  Here is an example they give in the same document:

 sumAxBy = @(x, y) (A*x + B*y);

For your particular problem it would be:

 f = @(x,y) -y^2;

where x and y are only scalar quantities. (Of course the x argument is actually not used in this particular function.)

  Note: If you use the letter 'f' for this purpose, then it cannot be used as the name of an ordinary variable in your code. Otherwise matlab would get confused.

Roger Stafford

Subject: quiry

From: Roger Stafford

Date: 26 Jun, 2011 03:03:02

Message: 7 of 8

"sana kazmi" wrote in message <iu5ruh$1lu$1@newscl01ah.mathworks.com>...
> If h = 0.001, and N is positive, how can i get values from k1..k4
- - - - - - - - -
  I don't understand what your difficulty is with positive indices. Using the values of x(1) and y(1) you work your way forward to x(2) and y(2). From them you get x(3) and y(3), and then x(4) and y(4), and so forth.

  If you are trying to make x change in the negative direction, you should use a negative value for h. Whatever else you do, the indices absolutely must remain positive integers if you are to use matlab.

Roger Stafford

Subject: quiry

From: Steven_Lord

Date: 27 Jun, 2011 02:07:44

Message: 8 of 8



"Anna Qureshi" <annaqureshi@ymail.com> wrote in message
news:iu5b3s$l37$1@newscl01ah.mathworks.com...
> Hi,
> following is the program code that i am trying to run, however it seems
> that the variables in the for loop are not recognized. If i use the
> command disp(k1), there is an error undefined. please help me fix this.
>
> %4th ORDER RUNGE KUTTA METHOD
> function y = rk(x,y,k1,k2,k3,k4,h)
> clear;

You just erased the inputs that were passed into your function.

Get rid of the CLEAR call.

Other than this, see Roger and Nasser's suggestions in the other messages in
this thread.

*snip*

--
Steve Lord
slord@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

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