필터 지우기
필터 지우기

I need a help with my project!!

조회 수: 1 (최근 30일)
Abdullah Jizani
Abdullah Jizani 2016년 5월 16일
편집: Abdullah Jizani 2016년 5월 26일
Hello everyone,
I have a problem writing my function on matlab and it simply shows that when I start from v(1.1)is not possible and it should be a positive integer.
it is a very simple function including a loop but it is required. this function is supposed to calculate the velocity and time for each height.
this is what I have written so far.
function y=freefall(v,t)
g=9.81;
%Gravity in m^2/s
h=input('Please Enter the hieght\n')
for k=1:h
v(k)=sqrt(2*g*k);
end
for k=1:h
t(k)=sqrt(2*k/g);
end
plot(t,v)
xlabel('Time (s)')
ylabel('Velocity
(m/s)')
title('Free fall: velocity vs time')
  댓글 수: 1
Walter Roberson
Walter Roberson 2016년 5월 16일
Please show the complete error message, everything in red. Also please show the parameters you are passing to your routine.

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

채택된 답변

John D'Errico
John D'Errico 2016년 5월 16일
v is a vector. what do you expect v(1.1) to return? Where will the 1.1 element of that vector be found? (Between the 1st and 2nd elements, I assume, but that is not how MATLAB does indexing.)
Oh, and you spelled "height" incorrectly. :)
I'd also wonder why you pass in v and t into the function as arguments? You create both of those variables inside the function.
Instead, you should pass in h as an argument, then you would never need to use that silly input statement.
Finally, you return y as an argument from this function. But you never create y as a variable. Perhaps a better choice of arguments for the function would have been
function [v,t]=freefall(h)
now, you would use the function like this:
[v,t]=freefall(h);
and completely drop out that silly input statement.
  댓글 수: 3
John D'Errico
John D'Errico 2016년 5월 16일
편집: John D'Errico 2016년 5월 16일
The point is that students see input as a nice way to provide information. In fact, it is a terribly inefficient interface method. You will quickly get tired of responding to inputs. Instead, learn to use functions. For example, suppose you want to compute the mean of some array.
Which way seems to be a more useful interface:
m = [1 2 3 4];
M = mean(m)
M =
2.5
or perhaps:
m = input('Please input the damn array?');
Please input the damn array?[1 2 3 4]
M = mean(m)
M =
2.5
Both approaches result in the same mean. But one of them forces you to type at a prompt.
So learn to use functions that take arguments. Mean is one such simple function. You pass it an argument, it returns a result. Any function that you write yourself will work exactly the same way. You supply the inputs to a function, it returns outputs to you.
This is an efficient way to work. It is very nice, because then you can use functions in other functions. You leverage your work, because once a function is defined to do a small (general) task, then you can use it everywhere. It is nice because you write small, modular code, that is easy to debug. Once you know that one small piece works, then you can ignore it, and start on another piece. So functions as small, self contained code fragments become a very efficient way to write code.
Steven Lord
Steven Lord 2016년 5월 16일
And if you're required to accept values using input, separate the computational piece (which should be a function) from the interface piece (which may be a script or a function and which calls the computational piece.)

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

추가 답변 (1개)

parth pandya
parth pandya 2016년 5월 16일
Hi Abdullah, Please Format code using '{}Code' option.It will be easy.
I just want to give you few pointers regarding your question.
1) Why are you passing v & t ?? As you are doing nothing with that variables in function.
2) Also I will recommend pre allocate memory for v & t. like:
v = zeros(h,1);
t = zeros(h,1);
3) If you save your function file with same function name,then code is running without error.
  댓글 수: 2
Abdullah Jizani
Abdullah Jizani 2016년 5월 16일
Can you please tell me the the {}code is? This wasn't explained to us in class. I really appreciate it.
parth pandya
parth pandya 2016년 5월 16일
편집: parth pandya 2016년 5월 16일
I was referring for using code option in forum not in matlab software.please see image.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by