Using a for loop to subplot graph parabolas?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
VBBV
2022-1-31
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 个评论
VBBV
2022-1-31
It will also work without hold on in this case since you are using subplot unlike only plot command.
更多回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!