Filter löschen
Filter löschen

My function wont work?

1 Ansicht (letzte 30 Tage)
Manopka
Manopka am 18 Mai 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)

Antworten (3)

Image Analyst
Image Analyst am 18 Mai 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 Kommentar
Image Analyst
Image Analyst am 18 Mai 2013
So did you try it and verify that this works?

Melden Sie sich an, um zu kommentieren.


sami
sami am 18 Mai 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 Kommentare
Manopka
Manopka am 18 Mai 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.

Melden Sie sich an, um zu kommentieren.


per isakson
per isakson am 18 Mai 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.

Kategorien

Mehr zu MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by