Loop an if with different parameters

1 次查看(过去 30 天)
Hi, doing some practice codings and ran into a snag. So, i'm trying to plot(t,y) with different matrix 'e' values? Let's say my first 'e' would be [-1; -2; -3; -4], and second [-2; -3; -4; -5] and keeps increasing with increments of 1 until a maximum value of [-7; -8; -9; -10], and plot everything in the same graph? How would I go about this? Code is below
e = [-1; -2; -3; -4]
K = place(A,B,e);
tspan = 0:0.01:200;
if(c==-1)
y0=[0; 0; 0; 0];
[t,y] = ode45(@(t,y)Equation(y,m,M,l,g,b,-K*(y-[4; 0; 0; 0])),tspan,y0);
elseif(c==1)
y0=[-3; 0; pi+.1; 0];
[t,y] = ode45(@(t,y)Equation(y,m,M,l,g,b,-K*(y-[1; 0; pi; 0])),tspan,y0);
else
end
plot(t,y);

采纳的回答

Setsuna Yuuki.
Setsuna Yuuki. 2020-11-15
You can use for to graph more than once.
for n=1:7
e = -n:-1:-n-3 % e values [-1 -2 -3 -4]
e = e'; %[-1;-2;-3;-4]
%Your code....
if(c==-1)
y0=[0; 0; 0; 0];
[t,y] = ode45(@(t,y)Equation(y,m,M,l,g,b,-K*(y-[4; 0; 0; 0])),tspan,y0);
elseif(c==1)
y0=[-3; 0; pi+.1; 0];
[t,y] = ode45(@(t,y)Equation(y,m,M,l,g,b,-K*(y-[1; 0; pi; 0])),tspan,y0);
else
end
% Your code end
plot(t,y); hold on;
end

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by