How do i change to "function

clc,clf,clear
maxit=1000;
g=9.81; theta0=50*pi/180; v0=5; CR=0.83;
j=1;t(j)=0;x=0;y=0;
xx=x;yy=y;
plot(x,y,'o','MarkerFaceColor','b','MarkerSize',8)
xmax=8; axis([0 xmax 0 0.8])
M(1)=getframe;
dt=1/128;
j=1; xxx=0; iter=0;
while(1)
tt=0;
timpact=2*v0*sin(theta0)/g;
ximpact=v0*cos(theta0)*timpact;
while(1)
j=j+1;
h=dt;
if tt+h>timpact,h=timpact-tt;end
t(j)=t(j-1)+h;
tt=tt+h;
x=xxx+v0*cos(theta0)*tt;
y=v0*sin(theta0)*tt-0.5*g*tt^2;
xx=[xx x];yy=[yy y];
plot(xx,yy,':',x,y,'o','MarkerFaceColor','b','MarkerSize',8)
axis([0 xmax 0 0.8])
M(j)=getframe;
iter=iter+1;
if tt>=timpact, break, end
end
v0=CR*v0;
xxx=x;
if x>=xmax|iter>=maxit,break,end
end
pause
clf
axis([0 xmax 0 0.8])
movie(M,1,36)

1 Comment

Simple...you decide what inputs you want...and make it a function.

Sign in to comment.

Answers (1)

madhan ravi
madhan ravi on 3 Oct 2018
Edited: madhan ravi on 3 Oct 2018
function movie_example %an example you can modify inserting inputs theta0, v0 , CR and so on
%clc,clf,clear %EDITED after Guillaume's comment
maxit=1000;
g=9.81; theta0=50*pi/180; v0=5; CR=0.83;
j=1;t(j)=0;x=0;y=0;
xx=x;yy=y;
plot(x,y,'o','MarkerFaceColor','b','MarkerSize',8)
xmax=8; axis([0 xmax 0 0.8])
M(1)=getframe;
dt=1/128;
j=1; xxx=0; iter=0;
while(1)
tt=0;
timpact=2*v0*sin(theta0)/g;
ximpact=v0*cos(theta0)*timpact;
while(1)
j=j+1;
h=dt;
if tt+h>timpact,h=timpact-tt;end
t(j)=t(j-1)+h;
tt=tt+h;
x=xxx+v0*cos(theta0)*tt;
y=v0*sin(theta0)*tt-0.5*g*tt^2;
xx=[xx x];yy=[yy y];
plot(xx,yy,':',x,y,'o','MarkerFaceColor','b','MarkerSize',8)
axis([0 xmax 0 0.8])
M(j)=getframe;
iter=iter+1;
if tt>=timpact, break, end
end
v0=CR*v0;
xxx=x;
if x>=xmax|iter>=maxit,break,end
end
pause
%clf
axis([0 xmax 0 0.8])
movie(M,1,36)
end
Remark: Download the attached file and type movie_example in command window to test the function file

3 Comments

clear at the start of a function is completely pointless. And I would argue that clc and clf have also no place in a function.
"clear at the start of a function is completely pointless"
Not just pointless, but totally counterproductive and wastes a lot of time.
madhan ravi
madhan ravi on 3 Oct 2018
Edited: madhan ravi on 3 Oct 2018
Thank you both for valuable suggestion rectified the mistake.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Asked:

on 3 Oct 2018

Edited:

on 3 Oct 2018

Community Treasure Hunt

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

Start Hunting!