MATLAB Answers

# Trying to plot a function of time while a variable changes values at different time intervals

19 views (last 30 days)
David Scidmore on 29 Jan 2021
Commented: David Scidmore on 29 Jan 2021
As the title states I'm trying to plot a function (or two actually), while a variable changes at different variables. The program just continues to run and doesn't actually stop so it's stuck somewhere and I can't figure out where. Also would this work once it's able to complete? I don't like having the beta inside the loop, but get an error whiles outside.
clear all
clc
L_phi = -2.729;
L_sigma_a = -43.692;
lambda = L_phi;
tau = 1/L_phi;
t = [0:0.01:5];
for t <= 5
if t < 2.5
sigma_a = -3;
elseif t >=2.5
sigma_a = 0;
for beta = (L_sigma_a/L_phi).*sigma_a;
phidot = beta.*(1-exp(lambda.*t));
phi = beta.*(t-1/tau.*exp(tau.*t));
end
end
end
figure(1)
plot(t,phidot)
title('phidot vs time')
xlabel('seconds')
ylabel('degrees')
figure(2)
plot(t, phi)
title('phi vs time')
xlabel('seconds')
ylabel('degrees')
Unrecognized function or variable 'phidot'.
Error in HW1MAE503 (line 23)
plot(t,phidot)
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Answers (1)

VBBV on 29 Jan 2021
clear all
clc
L_phi = -2.729;
L_sigma_a = -43.692;
lambda = L_phi;
tau = 1/L_phi;
t = [0:0.01:5];
for i = 1:length(t)
if t(i) < 2.5
sigma_a = -3;
elseif t(i) >=2.5
sigma_a = 0;
end
beta = (L_sigma_a/L_phi)*sigma_a;
phidot(i) = beta*(1-exp(lambda*t(i)));
phi(i) = beta*(t(i)-1/tau*exp(tau*t(i)));
end
figure(1)
plot(t,phidot)
title('phidot vs time')
xlabel('seconds')
ylabel('degrees')
figure(2)
plot(t, phi)
title('phi vs time')
xlabel('seconds')
ylabel('degrees')
May be this is what you want ...
##### 2 CommentsShowHide 1 older comment
David Scidmore on 29 Jan 2021
New to this language and used to python. Lots of little changes. Thanks.

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!