For loops with subplots and a function with changing constants
Afficher commentaires plus anciens
How would I go about using for loops to create subplots of a function while also changing the value of a constant for each subplot?
I'm going to simplify my equations in the example.
Say I want to create 3 subplots of the function y=a*x. The values of a are 2, 4, and 6. In subplot 1 I want to plot y=2x, in subplot 2 I want to plot y=4x, and in subplot 3 I want to plot y=6x. The values of x are 1:10.
I tried the following:
x = [0:1:10];
for i=1:3
subplot(1,3,i)
for a=[2 4 6];
y=a*x;
plot(x,y)
end
end
But that just gave me 3 identical plots for y=6x.
Réponse acceptée
Plus de réponses (1)
Patrick Hölken
le 21 Déc 2020
Hey!
I have a pretty similar problem. I want to add a second function in the loop, like y=a(i)*x.^2. Is it possible to create a figure which includes all the 6 plots (subplot(2,3,i))?
I tried this:
x = [0:1:10];
a = [2 4 6] ;
for i=1:3
subplot(2,3,i)
y=a(i)*x ;
hold on
subplot(2,3,i)
d=a(i)*x.^2;
plot(x,y)
plot(x,d)
end
But this gives me only 3 plots with 2 functions in each plot.
Catégories
En savoir plus sur Subplots dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!