Plotting every iteration of a while loop with multiple if statements
12 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
I have a while loop that has 3 if statements within it. For example while x>0 && x<5 and 3 if statements solving for y with different formulas within that range of 0-5. I need to plot every iteration of that range and cannot figure out how to do so.
Thanks!
3 comentarios
Sean Sullivan
el 22 de Mzo. de 2018
Editada: James Tursa
el 22 de Mzo. de 2018
VBBV
el 6 de Mzo. de 2022
x = 0.5;
while x>0 && x<6
x = x+0.5;
if x<4
syms y
eqn = y==2*x;
y = solve(eqn,y);
plot(x,y,'bo','MarkerFaceColor','b')
hold on
end
if x>4
syms y
eqn = y==0.5*x;
y = solve(eqn,y)
plot(x,y,'ro','MarkerFaceColor','r')
hold on
end
if strcmp(x,4)
syms y
eqn = y==5*x;
y = solve(eqn,y)
plot(x,y,'yo','MarkerFaceColor','y')
end
end
Respuestas (1)
Ameer Hamza
el 24 de Abr. de 2018
From our code in the comment , it appears that you don't need solve() function. You just have one equation and you already know the value of independent variable x. You can write your code as
l = line;
l.XData = [];
l.YData = [];
x = 0.5;
while x>0 && x<6
x = x+0.5;
if x <4
y = 2*x;
l.XData = [l.XData x];
l.YData = [l.YData y];
end
if x>4
y = 0.5*x;
l.XData = [l.XData x];
l.YData = [l.YData y];
end
if x == 4
y = 5*x;
l.XData = [l.XData x];
l.YData = [l.YData y];
end
end
This will also plot values of x and y.
0 comentarios
Ver también
Categorías
Más información sobre Calculus en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
