Variable Usage in Function File
Show older comments
Unfortunately, I still am not very knowledgeable about using "function" commands in Matlab. As such, I have two questions.
1) In this short script, I want to save vel() so I can plot it at the end. But when the script completes, the variable workspace is wiped clean; thus vel() is gone. How can I retain this variable after script completion?
function vend=velocity1(dt, ti,tf,vi)
t=ti;
v=vi;
n=(tf-ti)/dt;
for i=1:n
dvdt=deriv(v);
v=v+dvdt*dt;
vel(i,1)=v;
end
vend=v;
end
function dv=deriv(v)
dv=9.81-(0.25/68.1)*v^2;
end
2) With the same script above, why does the software give me an error if I add a prefix to it as shown in the following? These input variables need defined, so I'm not sure why the software would give an error.
dt=0.5;
ti=0;
tf=12;
vi=0;
function vend=velocity1(dt, ti,tf,vi)
.
.
.
<same script as above>
Thanks in advance, M Ridzon
Accepted Answer
More Answers (0)
Categories
Find more on Variables 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!