Using a for loop to subplot graph parabolas?
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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.
0 Kommentare
Akzeptierte Antwort
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
function y = parabolafx(x,a,b,c)
y= a*x.^2+b*x+c;
end
Try some thing like this
3 Kommentare
VBBV
am 31 Jan. 2022
It will also work without hold on in this case since you are using subplot unlike only plot command.
Weitere Antworten (1)
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
Siehe auch
Kategorien
Mehr zu Subplots finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!