My function wont work?

1 view (last 30 days)
Manopka
Manopka on 18 May 2013
Whats wrong with this function? it keep ssayinf variables are undefined.
% thsi is the function I wrote
function [maxheight, time, speed] = cannon(x0, y0, v0,angle)
maxheight=(v0^2*sin(angle)^2)/(2*9.8)
time=v0*sin(angle)/9.8
speed=cos(angle)*v0
endfunction
%this is the code given by the professor
x0 = 0;
y0 = 0;
v0 = 20; % initial speed in m/s
angle = 45; % elevation angle in degrees
[maxheight,time,speed] = cannon(x0,y0,v0,angle);
fprintf('The max height is %7.2f \n' , maxheight)
fprintf('The time of the max height is %7.2f seconds \n' , time)
fprintf('The speed at the max height is %7.2f \n' , speed)

Answers (3)

Image Analyst
Image Analyst on 18 May 2013
Get rid of "endfunction" and it will run fine. Copy this (both functions) into a file called test.m and run it:
function test
%this is the code given by the professor
x0 = 0;
y0 = 0;
v0 = 20; % initial speed in m/s
angle = 45; % elevation angle in degrees
[maxheight,time,speed] = cannon(x0,y0,v0,angle);
fprintf('The max height is %7.2f \n' , maxheight)
fprintf('The time of the max height is %7.2f seconds \n' , time)
fprintf('The speed at the max height is %7.2f \n' , speed)
function [maxheight, time, speed] = cannon(x0, y0, v0,angle)
maxheight=(v0^2*sin(angle)^2)/(2*9.8)
time=v0*sin(angle)/9.8
speed=cos(angle)*v0
It will run fine.
  1 Comment
Image Analyst
Image Analyst on 18 May 2013
So did you try it and verify that this works?

Sign in to comment.


sami
sami on 18 May 2013
1) Open matlab-> create new m-file
copy-paste this : function [maxheight, time, speed] = cannon(x0, y0, v0,angle)
maxheight=(v0^2*sin(angle)^2)/(2*9.8);
time=v0*sin(angle)/9.8;
speed=cos(angle)*v0;
end
Ctrl+S (to save it) save it as it is (autodetection of the name of the function is the same as the name of the file.
2) create another new m-file :
copy paste this :
clear all;clc
%this is the code given by the professor x0 = 0; y0 = 0; v0 = 20; % initial speed in m/s angle = 45; % elevation angle in degrees [maxheight,time,speed] = cannon(x0,y0,v0,angle); fprintf('The max height is %7.2f \n' , maxheight) fprintf('The time of the max height is %7.2f seconds \n' , time) fprintf('The speed at the max height is %7.2f \n' , speed)
Ctrl+S (give it a name)
Then run it.
RESULT
The max height is 14.78 The time of the max height is 1.74 seconds The speed at the max height is 10.51
END OF RESULT
  2 Comments
Manopka
Manopka on 18 May 2013
thank you. this is what i was doing wrong.
the professor told us to paste his code into our file but thats only for when we turn it in.

Sign in to comment.


per isakson
per isakson on 18 May 2013
"it keep ssayinf variables are undefined". What is the exact error message?
I cannot guess what you are doing to produce an error
Running this script
%%this is the code given by the professor
x0 = 0;
y0 = 0;
v0 = 20; % initial speed in m/s
angle = 45; % elevation angle in degrees
[maxheight,time,speed] = cannon(x0,y0,v0,angle);
fprintf('The max height is %7.2f \n' , maxheight)
fprintf('The time of the max height is %7.2f seconds \n' , time)
fprintf('The speed at the max height is %7.2f \n' , speed)
prints the following in the command window
The max height is 14.78
The time of the max height is 1.74 seconds
The speed at the max height is 10.51
where
function [ maxheight, time, speed] = cannon( x0, y0, v0, angle )
maxheight=( v0^2*sin(angle)^2)/(2*9.8);
time=v0*sin(angle)/9.8;
speed=cos(angle)*v0;
end
The input arguments, x0 and y0, are not used.

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!