Putting user input values outside function and having ode45 incorporate them in equations?
Show older comments
% code
g=9.8;
%Mass 1
promptm1='Enter a value for mass 1(between 1 and 4) ';
m1=input(promptm1);
while (m1<=0)||(m1>=5)
disp('Input is outside specified range. Try again.')
promptm1='Enter a value for mass 1(between 1 and 4) ';
m1=input(promptm1);
if (m1>0)&&(m1<5)
fprintf('Mass 1 will be: %2.0f \n',m1)
end
end
%Mass 2
promptm2='Enter a value for mass 2(between 1 and 4) ';
m2=input(promptm2);
while (m2<=0)||(m2>=5)
disp('Input is outside specified range. Try again. ')
promptm2='Enter a value for mass 2(between 1 and 4) ';
m2=input(promptm2);
if (m2>0)&&(m2<5)
fprintf('Mass 2 will be: %2.0f \n',m2)
end
end
%Length 1
promptl1='Enter a value for length 1(between 1 and 4) ';
L1=input(promptl1);
while (L1<=0)||(L1>=5)
disp('Input is outside specified range. Try again.')
promptl1='Enter a value for mass 1(between 1 and 4) ';
L1=input(promptl1);
if (L1>0)&&(L1<5)
fprintf('Length 1 will be: %2.0f \n',L1)
end
end
%Length 2
promptl2='Enter a value for length 2(between 1 and 4) ';
L2=input(promptl2);
while (L2<=0)||(L2>=5)
disp('Input is outside specified range. Try again.')
promptl2='Enter a value for length 2(between 1 and 4 )';
L2=input(promptl2);
if (L2>0)&&(L2<5)
fprintf('Length 2 will be: %2.0f \n',L2)
end
end
%Time
prompt= 'Enter how long you would like the double pendulum to swing (in seconds)';
T=input(prompt);
y0=[pi/2 0 pi 0];
%%Double Pendulum Equations
[t,y]=ode45(@pend, [0 T], y0);
end
This is the funtion file (pend.m)
% code
function [yprime] = pend(t, y)
g=9.8;
yprime=zeros(4,1);
a = (m1+m2)*l1 ;
b = m2*l2*cos(y(1)-y(3)) ;
c = m2*l1*cos(y(1)-y(3)) ;
d = m2*l2 ;
e = -m2*l2*y(4)* y(4)*sin(y(1)-y(3))-g*(m1+m2)*sin(y(1)) ;
f = m2*l1*y(2)*y(2)*sin(y(1)-y(3))-m2*g*sin(y(3)) ;
yprime(1) = y(2);
yprime(3)= y(4) ;
yprime(2)= (e*d-b*f)/(a*d-c*b) ;
yprime(4)= (a*f-c*e)/(a*d-c*b) ;
yprime=yprime';
end
end
I cant figure out how to make the m1,m2,l1,l2 inside the function equal the m1,m2,l1,l2 defined user inout variables outside the function. Please help asap
Answers (1)
Walter Roberson
on 14 Dec 2016
0 votes
Categories
Find more on Ordinary Differential Equations 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!