Filter löschen
Filter löschen

Using a for loop to subplot graph parabolas?

1 Ansicht (letzte 30 Tage)
Lee
Lee am 31 Jan. 2022
Kommentiert: VBBV am 31 Jan. 2022
I'm new to matlab and need some help with an assignement. I'm being asked to use a for loop to create a subplot with 12 graphs. I have been given a matrix (called coeffs) that is 3X12, column 1 has a data, column 2 has b data and column 3 has c data. The parabola function is y= a*x.^2+b*x+c. In a seperate tab that has been saved I said:
function y = parabolafx(x,a,b,c)
y= a*x.^2+b*x+c;
In my plot tab I have:
figure
x=-5:.1:5
for a=1:12
ylim([-100 100])
subplot(4,3,a)
bah=parabolafx(x,a,b,c)
plot(x,bah)
end
What I'm trying to do is have one parabola per subplot. In this case I have 12 in each of the 12 subplots. A, b and c were all created by giving each variable its own row from the given data.
I want the for loop to run through and give me one parabola per subplot and I want it to pull a, b and c from a given set of data called "coeffs".
I recognize that I am very lost. Please help.

Akzeptierte Antwort

VBBV
VBBV am 31 Jan. 2022
figure
x=-5:.1:5;
a=[1.50000000000000,2,-2,2,0.500000000000000,-2,-1,1.50000000000000,2.50000000000000,2.50000000000000,-1.50000000000000,2.50000000000000];
b=[10,0,6,-8,-2,8,6,10,4,-10,6,8];
c=[6,9,6,-3,6,-9,6,-15,-6,-15,-12,9];
for i=1:12 % change this index
ylim([-100 100])
subplot(4,3,i)
bah=parabolafx(x,a(i),b(i),c(i));
plot(x,bah)
hold on
end
bah = 1×101
-6.5000 -6.9850 -7.4400 -7.8650 -8.2600 -8.6250 -8.9600 -9.2650 -9.5400 -9.7850 -10.0000 -10.1850 -10.3400 -10.4650 -10.5600 -10.6250 -10.6600 -10.6650 -10.6400 -10.5850 -10.5000 -10.3850 -10.2400 -10.0650 -9.8600 -9.6250 -9.3600 -9.0650 -8.7400 -8.3850
bah = 1×101
59.0000 57.0200 55.0800 53.1800 51.3200 49.5000 47.7200 45.9800 44.2800 42.6200 41.0000 39.4200 37.8800 36.3800 34.9200 33.5000 32.1200 30.7800 29.4800 28.2200 27.0000 25.8200 24.6800 23.5800 22.5200 21.5000 20.5200 19.5800 18.6800 17.8200
bah = 1×101
-74.0000 -71.4200 -68.8800 -66.3800 -63.9200 -61.5000 -59.1200 -56.7800 -54.4800 -52.2200 -50.0000 -47.8200 -45.6800 -43.5800 -41.5200 -39.5000 -37.5200 -35.5800 -33.6800 -31.8200 -30.0000 -28.2200 -26.4800 -24.7800 -23.1200 -21.5000 -19.9200 -18.3800 -16.8800 -15.4200
bah = 1×101
87.0000 84.2200 81.4800 78.7800 76.1200 73.5000 70.9200 68.3800 65.8800 63.4200 61.0000 58.6200 56.2800 53.9800 51.7200 49.5000 47.3200 45.1800 43.0800 41.0200 39.0000 37.0200 35.0800 33.1800 31.3200 29.5000 27.7200 25.9800 24.2800 22.6200
bah = 1×101
28.5000 27.8050 27.1200 26.4450 25.7800 25.1250 24.4800 23.8450 23.2200 22.6050 22.0000 21.4050 20.8200 20.2450 19.6800 19.1250 18.5800 18.0450 17.5200 17.0050 16.5000 16.0050 15.5200 15.0450 14.5800 14.1250 13.6800 13.2450 12.8200 12.4050
bah = 1×101
-99.0000 -96.2200 -93.4800 -90.7800 -88.1200 -85.5000 -82.9200 -80.3800 -77.8800 -75.4200 -73.0000 -70.6200 -68.2800 -65.9800 -63.7200 -61.5000 -59.3200 -57.1800 -55.0800 -53.0200 -51.0000 -49.0200 -47.0800 -45.1800 -43.3200 -41.5000 -39.7200 -37.9800 -36.2800 -34.6200
bah = 1×101
-49.0000 -47.4100 -45.8400 -44.2900 -42.7600 -41.2500 -39.7600 -38.2900 -36.8400 -35.4100 -34.0000 -32.6100 -31.2400 -29.8900 -28.5600 -27.2500 -25.9600 -24.6900 -23.4400 -22.2100 -21.0000 -19.8100 -18.6400 -17.4900 -16.3600 -15.2500 -14.1600 -13.0900 -12.0400 -11.0100
bah = 1×101
-27.5000 -27.9850 -28.4400 -28.8650 -29.2600 -29.6250 -29.9600 -30.2650 -30.5400 -30.7850 -31.0000 -31.1850 -31.3400 -31.4650 -31.5600 -31.6250 -31.6600 -31.6650 -31.6400 -31.5850 -31.5000 -31.3850 -31.2400 -31.0650 -30.8600 -30.6250 -30.3600 -30.0650 -29.7400 -29.3850
bah = 1×101
36.5000 34.4250 32.4000 30.4250 28.5000 26.6250 24.8000 23.0250 21.3000 19.6250 18.0000 16.4250 14.9000 13.4250 12.0000 10.6250 9.3000 8.0250 6.8000 5.6250 4.5000 3.4250 2.4000 1.4250 0.5000 -0.3750 -1.2000 -1.9750 -2.7000 -3.3750
bah = 1×101
97.5000 94.0250 90.6000 87.2250 83.9000 80.6250 77.4000 74.2250 71.1000 68.0250 65.0000 62.0250 59.1000 56.2250 53.4000 50.6250 47.9000 45.2250 42.6000 40.0250 37.5000 35.0250 32.6000 30.2250 27.9000 25.6250 23.4000 21.2250 19.1000 17.0250
bah = 1×101
-79.5000 -77.4150 -75.3600 -73.3350 -71.3400 -69.3750 -67.4400 -65.5350 -63.6600 -61.8150 -60.0000 -58.2150 -56.4600 -54.7350 -53.0400 -51.3750 -49.7400 -48.1350 -46.5600 -45.0150 -43.5000 -42.0150 -40.5600 -39.1350 -37.7400 -36.3750 -35.0400 -33.7350 -32.4600 -31.2150
bah = 1×101
31.5000 29.8250 28.2000 26.6250 25.1000 23.6250 22.2000 20.8250 19.5000 18.2250 17.0000 15.8250 14.7000 13.6250 12.6000 11.6250 10.7000 9.8250 9.0000 8.2250 7.5000 6.8250 6.2000 5.6250 5.1000 4.6250 4.2000 3.8250 3.5000 3.2250
function y = parabolafx(x,a,b,c)
y= a*x.^2+b*x+c;
end
Try some thing like this
  3 Kommentare
VBBV
VBBV am 31 Jan. 2022
It prevents the graphs from being overwritten during loop iteration
VBBV
VBBV am 31 Jan. 2022
It will also work without hold on in this case since you are using subplot unlike only plot command.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Arif Hoq
Arif Hoq am 31 Jan. 2022
Bearbeitet: Arif Hoq am 31 Jan. 2022
Fucntion part:
function y = parabolafx(x,a,b,c)
y= a*x.^2+b*x+c;
end
you need ot initialize the value of b and c. I have initilized randomly.
figure(1)
b=10;
c=2;
x=-5:0.1:5;
for a=1:12
ylim([-100 100])
subplot(4,3,a)
bah=parabolafx(x,a,b,c);
plot(x,bah)
end
  4 Kommentare
Arif Hoq
Arif Hoq am 31 Jan. 2022
VBBV already solved your problem, i guess..
Lee
Lee am 31 Jan. 2022
Thank you so much for your help :)

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by