Undefined function or variable 'm'

Good evening, I am a fairly new matlab user and am trying to run ode45 to output a graph of a change in theta over time. I am using a friends code as a template and can't figure out where I am going wrong. I appreciate the help that anyone can offer. For context, the problem is of someone rappelling from a helicopter, and I want to graph the change in theta with different values for mass, wind force (Fw), and Length.
global Fw, m, g, L
R2D= 180/pi;
m= 97; % mass of soldier [Kg]
L= -5; % Length of soldier on rope (relative to helicopter)
g= 9.81; % gravity in m/s^2
dt=.01; % step size for time
tspan= 0:dt:tf;
the= 1*(pi/180); %initial angle in degrees
y0=[0, 0, the, 0];
[t,x]= ode45(@AA,tspan,y0);
figure
plot(t,x(:,1)*R2D)
title('Pendulum angle over time')
xlabel('Time [s]')
ylabel('\theta [deg]')
[n,m] = size(t);
W = -m*g.*ones(n,1);
function dxdt = AA(t,x)
global g L m Fw
dxdt = zeros(2,1);
dxdt(1) = x(2);
dxdt(2) = (Fw*cos(x(1)))/(m*L)-(g/L)*sin(x(1));
end

답변 (2개)

KSSV
KSSV 2017년 11월 29일

1 개 추천

Replace line:
global Fw, m, g, L
with :
global Fw m g L
don't use commas.

댓글 수: 7

There are other errors....this code works:
global Fw m g L
Fw = rand ;
R2D= 180/pi;
m = 97; % mass of soldier [Kg]
L= -5; % Length of soldier on rope (relative to helicopter)
g= 9.81; % gravity in m/s^2
dt=.01; % step size for time
tf = 100 ;
tspan= 0:dt:tf;
the= 1*(pi/180); %initial angle in degrees
y0=[0,the];
[t,x]= ode45(@PD,tspan,y0);
figure
plot(t,x(:,1)*R2D)
title('Pendulum angle over time')
xlabel('Time [s]')
ylabel('\theta [deg]')
[nx,ny] = size(t);
W = -m*g.*ones(nx,1);
end
function dxdt = PD(t,x)
global g L m Fw
dxdt = zeros(2,1);
dxdt(1) = x(2);
dxdt(2) = (Fw*cos(x(1)))/(m*L)-(g/L)*sin(x(1));
end
cory loob
cory loob 2017년 11월 29일
Thank you for your help. It does indeed work now, although it is a few thousand degrees too high. Any suggestions? Thank you again, I appreciate the speedy reply.
KSSV
KSSV 2017년 11월 29일
Dude..please note that I have used some random values for Fw..As you have not defined them, I had to make them random. you have to give your values......make a note of that....
cory loob
cory loob 2017년 11월 29일
Ya I noticed that, changed it to a normal value (12) and still end up with degrees in the thousands.
cory loob
cory loob 2017년 11월 29일
It's fixed. Gravity was 9.81, not -9.81.
KSSV
KSSV 2017년 11월 29일
Good......
Stephen23
Stephen23 2017년 11월 29일
And don't use globals. Search this forum to know why.

댓글을 달려면 로그인하십시오.

Chafik Zerrouki
Chafik Zerrouki 2017년 11월 29일

1 개 추천

Hello Cory,
Read the documentation of 'global'. Don't put comma and it will work (line 1).
global Fw m g L

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

태그

질문:

2017년 11월 29일

댓글:

2017년 11월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by