creating graphs from loops
Mostrar comentarios más antiguos
i have two equations that require a loop to solve them. i need to create a graph from the loop with both equations on the same axis. however every time i attempt to do this each value in the loop is overwritten and so the graph on has one point. how do i save each point in the loop. the graph needs to be (y1 y1, m)
for m = (60:70)
y1 = (14*35)/(m*9.8)
y2 = (1 - exp((-14*7)/m
end
Respuestas (2)
Turlough Hughes
el 4 de Oct. de 2019
Hi Georgia,
You actually dont need a loop at all. You can get the answer easily using piecewise multiplication and division instead:
m=60:70;
y1=(14*35)./(m.*9.8);
y2=1-exp((-14*7)./m);
figure(), plot(m,y1,m,y2), legend('y1','y2')
I made the same assumptions about parentheses as the cyclist and that you want to plot y1 and y2 against m.
the cyclist
el 4 de Oct. de 2019
Here is one way that I typically solve that:
mrange = 60:70;
mcount = numel(mrange);
y1 = zeros(mcount,1);
y2 = zeros(mcount,1);
for mi = 1:mcount
y1(mi) = (14*35)/(mrange(mi)*9.8);
y2(mi) = (1 - exp((-14*7)/mrange(mi)));
end
figure
plot(mrange,y1,mrange,y2)
Note that your line of code for y2 had syntax errors with mismatched parentheses, which I corrected without much thought to what you intended to calculate there.
Categorías
Más información sobre Graph and Network Algorithms en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!