Using a for loop to subplot graph parabolas?
4 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Respuesta aceptada
VBBV
el 31 de En. de 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 comentarios
VBBV
el 31 de En. de 2022
It will also work without hold on in this case since you are using subplot unlike only plot command.
Más respuestas (1)
Arif Hoq
el 31 de En. de 2022
Editada: Arif Hoq
el 31 de En. de 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 comentarios
Ver también
Categorías
Más información sobre 2-D and 3-D Plots 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!
