필터 지우기
필터 지우기

My function wont work?

조회 수: 1 (최근 30일)
Manopka
Manopka 2013년 5월 18일
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)

답변 (3개)

Image Analyst
Image Analyst 2013년 5월 18일
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
Image Analyst
Image Analyst 2013년 5월 18일
So did you try it and verify that this works?

댓글을 달려면 로그인하십시오.


sami
sami 2013년 5월 18일
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
Manopka
Manopka 2013년 5월 18일
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.

댓글을 달려면 로그인하십시오.


per isakson
per isakson 2013년 5월 18일
"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.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by