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:
I'm new to matlab; why is this function wrong?

Subject: I'm new to matlab; why is this function wrong?

From: Alex

Date: 6 Dec, 2012 16:22:08

Message: 1 of 6

I'm getting the following error:
Error: File: two_dimensional_ac.m Line: 4 Column: 1
Function definitions are not permitted in this context.
Code:
plot(0,0,'yX')
hold on

function [r v] = orbits(Ro, Vo, del_t)
    Ro=[1.5e11, 0];
    Vo=[0, 2*pi*Ro(1)/(365*24*60*60)];
    del_t=24*3600
    G = 1.67e-11;
    Ms = 1.97e30;
    d = (Ro(1)^2 + Ro(2)^2)^(3/2);
    a = -(G*Ms/d)* ro;
    v = vo + a * del_t;
    r = ro + vo * del_t + .5 * a * del_t^2;
for n = 1:1:730
    [r v] = orbits(ro, vo, del_t)
    plot(r(1),r(2),'go')
    hold on;
        ro=r;
        vo=v;
end;

Thank you!

 

Subject: I'm new to matlab; why is this function wrong?

From: Alex

Date: 6 Dec, 2012 16:36:08

Message: 2 of 6

Update to the Code:

 function [R, V] = orbits(Ro, Vo, del_t)
    Ro=[1.5e11, 0];
    Vo=[0, 2*pi*Ro(1)/(365*24*60*60)];
    del_t=24*3600;
    G = 1.67e-11;
    Ms = 1.97e30;
    d = (Ro(1)^2 + Ro(2)^2)^(3/2);
    a = -(G*Ms/d)* Ro;
    V = Vo + a * del_t;
    R = Ro + Vo * del_t + .5 * a * del_t^2;

for n = 1:1:730
    plot(0,0,'yX')
    hold on
    [R, V] = orbits(Ro, Vo, del_t)
    plot(R(1),R(2),'go')
    hold on;
        Ro=R;
        Vo=V;
end
end

Subject: I'm new to matlab; why is this function wrong?

From: Sargondjani

Date: 6 Dec, 2012 16:36:08

Message: 3 of 6

you can only define functions inside a function-file and not in a script (dont ask me why, because it bothered me too, but matlab developers usually have good reasons for doing such things)

so either:
1) transform your script into a function:
start with: "function my_script()" and end with "end"

2) define the function in an m-file that you call in your script

i hope this helps...

Subject: I'm new to matlab; why is this function wrong?

From: Alex

Date: 6 Dec, 2012 16:40:08

Message: 4 of 6

The program now brings up a plot, but it's blank. It is supposed to plot an X in the middle and an orbit of green o's.

It also brings up this error: ??? Undefined function or method 'orbits' for input arguments of type 'double'.

Error in ==> two_dimensional_ac at 15
    [R V] = orbits(Ro, Vo, del_t)

function [R V] = orbits(Ro, Vo, del_t)
    Ro=[1.5e11, 0];
    Vo=[0, 2*pi*Ro(1)/(365*24*60*60)];
    del_t=24*3600;
    G = 1.67e-11;
    Ms = 1.97e30;
    d = (Ro(1)^2 + Ro(2)^2)^(3/2);
    a = -(G*Ms/d)* Ro;
    V = Vo + a * del_t;
    R = Ro + Vo * del_t + .5 * a * del_t^2;

for n = 1:1:730
    plot(0,0,'yX')
    hold on
    [R V] = orbits(Ro, Vo, del_t) <----- Line 15 Error
    plot(R(1),R(2),'go')
    hold on;
        Ro=R;
        Vo=V;
end
end

Subject: I'm new to matlab; why is this function wrong?

From: Alex

Date: 6 Dec, 2012 16:54:08

Message: 5 of 6

"Sargondjani" wrote in message <k9qhho$obr$1@newscl01ah.mathworks.com>...
> you can only define functions inside a function-file and not in a script (dont ask me why, because it bothered me too, but matlab developers usually have good reasons for doing such things)
>
> so either:
> 1) transform your script into a function:
> start with: "function my_script()" and end with "end"
>
> 2) define the function in an m-file that you call in your script
>
> i hope this helps...

Thanks Sargondjani.

Now when I run this script:

plot(0,0,'yX')
hold on
for n = 1:1:730
    del_t=24*3600;
    [R V] = orbits(Ro, Vo, del_t);
    plot(R(1),R(2),'go')
    hold on;
    Ro=R;
    Vo=V;
end

calling function:

function [R V] = orbits(Ro, Vo, del_t)
    Ro=[1.5e11, 0];
    Vo=[0, 2*pi*Ro(1)/(365*24*60*60)];
    G = 1.67e-11;
    del_t=24*3600;
    Ms = 1.97e30;
    d = (Ro(1)^2 + Ro(2)^2)^(3/2);
    a = -(G*Ms/d)* Ro;
    V = Vo + a * del_t;
    R = Ro + Vo * del_t + .5 * a * del_t^2;
end

I get Error in ==> two_dimensional_ac at 5
    [R V] = orbits(Ro, Vo, del_t);
 

Subject: I'm new to matlab; why is this function wrong?

From: Sargondjani

Date: 6 Dec, 2012 17:41:08

Message: 6 of 6

It seems you are confused about the definition of inputs and outputs of functions:

a function should look like this:

function [output1,output2,...]=my_fun(input1,input2,...)

if you call this function you have to define input1, input2,... BEFORE you call the function.

When you call orbits the first time, there does not exist a Ro, so that gives an error

BUT in addition you overwrite all the input variables inside the function. you should not define Ro, Vo or del_t inside your functions if you want to take them as inputs (unless you want to transform these inputs, but i doubt that is what you want)

so you need to define initial values for Ro,Vo in the script (before the loop), and delete the values of del_t and Ro inside the function

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