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:
Help with function argument

Subject: Help with function argument

From: Alissa Kidd

Date: 17 Nov, 2010 22:06:05

Message: 1 of 5

Hi. Alright so I'm VERY new to matlab and I'm trying to write a code for a project I have to do. For the code I have I keep getting this error message:

??? Error: File: guessTraj.m Line: 7 Column: 1
Function definitions are not permitted at the prompt or in scripts.

And here is my code:


target_dis=input('distance :');

function [output]= guessTraj(target_dis)

error = sqrt(target_dis);

real = isreal(error);



if real == 0 & isnumeric(target)== 0

    output = 4

    disp('Invalid Distance')

end



    angle = input('Please enter the angle in degrees:');

    radians = angle * (3.14159/180);

    velocity = input('Please enter the velocity the ball is thrown:');

    vel_error = sqrt(velocity);

    vel_check = isreal(vel_error);



    if vel_check == 0

        disp('Invalid Velocity')

    end



    VY = velocity * sin(radians) ;

    VX = velocity * cos(radians) ;

    t = (2*velocity)/9.81 ;

    x_dis = VX * t ;

    y_dis = VY * t ;

 

    H1 = target_dis + 2.5 ;

    H2 = target_dis - 2.5 ;

    

    x = 0:x_dis/50:x_dis;

    plot(x,x_dis,target_dis,0,'mo')

    xlabel ('Distance the ball Traveled')

    ylabel ('Height of the ball')

    legend ('Trajectory of Ball', 'Location', 'SouthEastOutside')

    hold on

    

    timeVector = 0:t/50:t;

    plot('b-')

        if x_dis <= H1 && x_dis >= H2

           title('The ball hit the target')

           output = 1;

           disp('1. The ball hit the target!!!!!')

     

        elseif x_dis < H1

            title('The ball was underthrown')

            under = target_dis-x_dis;

            output = 2;

            disp('2. The ball was underthrown')

      

        elseif x_dis > H2

            title('The ball was overthrown')

            over = x_dis-target_dis;

            output = 3;

            disp('3. The ball was overthrown')

        end



Now I know that the error message is because of the target_dis=input('distance :'); because at first I didn't have this, but I was getting this error message:

??? Input argument "target_dis" is undefined.

Error in ==> guessTraj at 19
error = sqrt(target_dis);

I want the user to enter that (and by that I mean the target_dis) but I am not sure how I am supposed to have a user enter something and then use it as my function arguement. Any help would be great!

-Thank you

Subject: Help with function argument

From: Sean de

Date: 17 Nov, 2010 22:21:03

Message: 2 of 5

"Alissa Kidd" <mooncather@yahoo.com> wrote in message <ic1jkd$qv0$1@fred.mathworks.com>...
> Hi. Alright so I'm VERY new to matlab and I'm trying to write a code for a project I have to do. For the code I have I keep getting this error message:
>
> ??? Error: File: guessTraj.m Line: 7 Column: 1
> Function definitions are not permitted at the prompt or in scripts.
>
> And here is my code:
>
>
> target_dis=input('distance :');
>
> function [output]= guessTraj(target_dis)
>
> error = sqrt(target_dis);
>
> real = isreal(error);
>
>
>
> if real == 0 & isnumeric(target)== 0
>
> output = 4
>
> disp('Invalid Distance')
>
> end
>
>
>
> angle = input('Please enter the angle in degrees:');
>
> radians = angle * (3.14159/180);
>
> velocity = input('Please enter the velocity the ball is thrown:');
>
> vel_error = sqrt(velocity);
>
> vel_check = isreal(vel_error);
>
>
>
> if vel_check == 0
>
> disp('Invalid Velocity')
>
> end
>
>
>
> VY = velocity * sin(radians) ;
>
> VX = velocity * cos(radians) ;
>
> t = (2*velocity)/9.81 ;
>
> x_dis = VX * t ;
>
> y_dis = VY * t ;
>
>
>
> H1 = target_dis + 2.5 ;
>
> H2 = target_dis - 2.5 ;
>
>
>
> x = 0:x_dis/50:x_dis;
>
> plot(x,x_dis,target_dis,0,'mo')
>
> xlabel ('Distance the ball Traveled')
>
> ylabel ('Height of the ball')
>
> legend ('Trajectory of Ball', 'Location', 'SouthEastOutside')
>
> hold on
>
>
>
> timeVector = 0:t/50:t;
>
> plot('b-')
>
> if x_dis <= H1 && x_dis >= H2
>
> title('The ball hit the target')
>
> output = 1;
>
> disp('1. The ball hit the target!!!!!')
>
>
>
> elseif x_dis < H1
>
> title('The ball was underthrown')
>
> under = target_dis-x_dis;
>
> output = 2;
>
> disp('2. The ball was underthrown')
>
>
>
> elseif x_dis > H2
>
> title('The ball was overthrown')
>
> over = x_dis-target_dis;
>
> output = 3;
>
> disp('3. The ball was overthrown')
>
> end
>
>
>
> Now I know that the error message is because of the target_dis=input('distance :'); because at first I didn't have this, but I was getting this error message:
>
> ??? Input argument "target_dis" is undefined.
>
> Error in ==> guessTraj at 19
> error = sqrt(target_dis);
>
> I want the user to enter that (and by that I mean the target_dis) but I am not sure how I am supposed to have a user enter something and then use it as my function arguement. Any help would be great!
>
> -Thank you

You need to have the function declaration line be the first one in your file:

function [output]= guessTraj(target_dis)
target_dis=input('distance :');



Then call it at the command line or in another m-file
gaussTraj(37) %37 is just a random number.

Subject: Help with function argument

From: Alissa Kidd

Date: 17 Nov, 2010 22:43:04

Message: 3 of 5

Okay I did that.
However, now I am getting another error message.

??? Error using ==> plot
Invalid first data argument

Error in ==> guessTraj at 89
    plot('b-')


...is the whole logic of my program wrong, or is this just a simple mistake that I can fix..?

Subject: Help with function argument

From: Sean de

Date: 17 Nov, 2010 22:50:07

Message: 4 of 5

"Alissa Kidd" <mooncather@yahoo.com> wrote in message <ic1lpo$jc2$1@fred.mathworks.com>...
> Okay I did that.
> However, now I am getting another error message.
>
> ??? Error using ==> plot
> Invalid first data argument
>
> Error in ==> guessTraj at 89
> plot('b-')
>
>
> ...is the whole logic of my program wrong, or is this just a simple mistake that I can fix..?

syntax error.

doc plot

to see how it's supposed to be used.

Subject: Help with function argument

From: Steven_Lord

Date: 18 Nov, 2010 14:58:19

Message: 5 of 5



"Alissa Kidd" <mooncather@yahoo.com> wrote in message
news:ic1lpo$jc2$1@fred.mathworks.com...
> Okay I did that. However, now I am getting another error message.
>
> ??? Error using ==> plot
> Invalid first data argument
>
> Error in ==> guessTraj at 89
> plot('b-')
>
>
> ...is the whole logic of my program wrong, or is this just a simple
> mistake that I can fix..?

You need to tell PLOT what data to plot rather than simply telling it how
you want the plot to look; it can't read your mind and determine which
variables you want displayed.

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