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

 Respuesta aceptada

Star Strider
Star Strider el 7 de Jun. de 2014
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 comentarios

MA
MA el 7 de Jun. de 2014
please write whole program,i don't understand
Star Strider
Star Strider el 7 de Jun. de 2014
Editada: Star Strider el 7 de Jun. de 2014
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.
MA
MA el 7 de Jun. de 2014
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
Star Strider
Star Strider el 7 de Jun. de 2014
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.)
MA
MA el 7 de Jun. de 2014
thank you so much
Star Strider
Star Strider el 7 de Jun. de 2014
My pleasure!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Preguntada:

MA
el 7 de Jun. de 2014

Comentada:

el 7 de Jun. de 2014

Community Treasure Hunt

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

Start Hunting!

Translated by