How can i plot f(i),g(i),h(i) in one figure(i)?

 Accepted Answer

Use the subplot function.
Your loop becomes:
figure(1)
for i = 1:3
subplot(3,1,i)
. . .
plot(x,f(i),'-b', ... )
. . .
end
to get 3 stacked plots in one figure. Other configurations are possible. See the documentation on subplot for details.

6 Comments

please write whole program,i don't understand
It would have been easier if you had attached your code as something I could copy and paste. I went to the effort of typing it in, troubleshooting it when your initial code didn’t work, and correcting it.
This works as you wrote it, with my changes to get it to run:
syms x
f = sym(zeros(3,1));
g = sym(zeros(3,1));
h = sym(zeros(3,1));
f(1) = sin(x) + cos(x);
f(2) = tan(x);
f(3) = 1/(1+x);
g(1) = sin(2*x);
g(2) = cos(x)^2;
g(3) = log(x);
h(1) = x^3+sin(x^3);
h(2) = tan(x)^2;
h(3) = x;
ff = matlabFunction(f); % Convert symbolic vector to function
gf = matlabFunction(g); % Convert symbolic vector to function
hf = matlabFunction(h); % Convert symbolic vector to function
x = 0:0.5:10;
fgh = [ff(x)' gf(x)' hf(x)']; % Refer to this matrix in the loop
figure(1) % Create figure object
for i = 1:3
subplot(3,1,i) % Plot first of stacked subolots
plot(x, fgh(:,i+[0 3 6])) % Plot each function as function of ‘x’
axis equal
grid on
xlabel('x')
title( sprintf('Plot of f_{%d}, g_{%d}, h_{%d}', [i i i]))
end
To understand the changes I made (and the reasons) see the documentation on matlabFunction to understand those lines, and cell to understand my use of the curly braces ‘{}’ in my fgh assignment array creation.
it's absolutly correct but i want to put f(1),g(1) and h(1) on one diagram not to put f(1),f(2) and f(3) in one,and others similarly
I edited the code in my previous comment rather than re-post different code here. It should now do what you want.
The plots look strange because you have ‘axis equal’ in your plot statements. I left that in because it was in your original code. (I took it out when I ran it to be sure everything was plotting correctly.)
thank you so much

Sign in to comment.

More Answers (0)

Asked:

MA
on 7 Jun 2014

Commented:

on 7 Jun 2014

Community Treasure Hunt

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

Start Hunting!