changing a figure to a subplot
显示 更早的评论
Currently stuck... This code below creates a figure and three subplots as I have designed it too. I cannot figure out how to change the figure in the while loop to a subplot with the other subplots below.
Any help is greatly appreciated.
%--------------------Value Function Interation-----------------------------
%Start Timer
tic
Vold=zeros(1,n);
Iteration = 0;
error = 1;
while error>tol
[Vnew g] = max(utility+beta*Vold'*ones(1,n));
error = max(abs(Vold - Vnew))
Vold = Vnew;
Iteration = Iteration + 1
%Set up convergence graphics -- optional
hold on;
plot(kgrid,Vold)
xlabel('k')
ylabel('V(k) Iterations')
title('The Convergence Process')
hold off;
end
k= kgrid;
kp = kgrid(g);
c = f(k)+(1-delta)*k-kp;
%End Timer
toc
%---------------------Create Plots for Results----------------------------
%Plot
scrsz = get(0,'ScreenSize');
figure('Position',[scrsz(3)*1/4 scrsz(4)*1/4 scrsz(3)*1/2 ...
scrsz(4)*1/2]);
subplot(2,2,1)
plot(kgrid,Vold)
xlabel('k');
ylabel('V(k)');
title('Value Function');
subplot(2,2,2)
plot(kgrid,[kgrid; step*g]);
xlabel('k');
ylabel('g(k)');
title('Policy Function');
legend('45 degree-line','Policy Function','Location','Best');
subplot(2,2,3)
plot(kgrid,c);
xlabel('k');
ylabel('c(k)');
title('Consumption');
2 个评论
Chad Greene
2015-2-8
I'm not exactly sure what you're trying to do. Can you clarify your question?
per isakson
2015-2-8
编辑:per isakson
2015-2-8
Why did you include that much code? The latter part is more than enough. I cannot run the code without a lot of guessing regarding input values.
"error = 1;"   It is not a good idea to use names of functions to name variables.
采纳的回答
更多回答(1 个)
Image Analyst
2015-2-8
If you want "to change the figure in the while loop to a subplot" then you need to call subplot() in the while loop, so you need to estimate how many plots you will be creating and then make an array for that. Let's say that you might possibly have 90 plots. Then those could fit into a 9 row by 10 column array of plots. So in the while loop you'd do this:
subplot(9, 10, Iteration);
plot(kgrid,Vold)
That way, each plot will go to its own subplot instead of a single plot that takes up the whole figure and gets replaced/overwritten each iteration like you're getting now.
类别
在 帮助中心 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!