I need a help with my project!!
1 view (last 30 days)
Show older comments
Abdullah Jizani
on 16 May 2016
Edited: Abdullah Jizani
on 26 May 2016
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 Comment
Walter Roberson
on 16 May 2016
Please show the complete error message, everything in red. Also please show the parameters you are passing to your routine.
Accepted Answer
John D'Errico
on 16 May 2016
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 Comments
John D'Errico
on 16 May 2016
Edited: John D'Errico
on 16 May 2016
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
on 16 May 2016
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.)
More Answers (1)
parth pandya
on 16 May 2016
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 Comments
parth pandya
on 16 May 2016
Edited: parth pandya
on 16 May 2016
I was referring for using code option in forum not in matlab software.please see image.
See Also
Categories
Find more on Loops and Conditional Statements 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!