Filter löschen
Filter löschen

I need a help with my project!!

1 Ansicht (letzte 30 Tage)
Abdullah Jizani
Abdullah Jizani am 16 Mai 2016
Bearbeitet: Abdullah Jizani am 26 Mai 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 Kommentar
Walter Roberson
Walter Roberson am 16 Mai 2016
Please show the complete error message, everything in red. Also please show the parameters you are passing to your routine.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

John D'Errico
John D'Errico am 16 Mai 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 Kommentare
John D'Errico
John D'Errico am 16 Mai 2016
Bearbeitet: John D'Errico am 16 Mai 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
Steven Lord am 16 Mai 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.)

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

parth pandya
parth pandya am 16 Mai 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 Kommentare
Abdullah Jizani
Abdullah Jizani am 16 Mai 2016
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 am 16 Mai 2016
Bearbeitet: parth pandya am 16 Mai 2016
I was referring for using code option in forum not in matlab software.please see image.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Noch keine Tags eingegeben.

Community Treasure Hunt

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

Start Hunting!

Translated by