楼主: wb123456
2596 9

[问答] 请坛子里高手帮我看看这个MATLAB程序怎么修改,谢谢! [推广有奖]

  • 2关注
  • 6粉丝

已卖:402份资源

学科带头人

9%

还不是VIP/贵宾

-

威望
0
论坛币
6547 个
通用积分
77.7774
学术水平
60 点
热心指数
67 点
信用等级
58 点
经验
1883 点
帖子
1659
精华
0
在线时间
1972 小时
注册时间
2006-11-3
最后登录
2025-11-7

楼主
wb123456 发表于 2011-4-15 23:49:33 |AI写论文

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
这是程序原文:
%%%%% Matlab program (.m) file that gives solutions to "Problem Set Zero"
%%%%% You should play with this file to learn simple Matlab commands.
%%%%% Use "help" at the command window to figure out what various commands
%%%%% do. Eg, type "help clear" to figure out what the next line does.
clear all
%%%%% QUESTION 1: STEADY STATE
%%%%% Declare parameter values
g     = 0.03;
n     = 0.01;
s     = 0.20;
alpha = 0.33;
delta = 0.04;
parameters = [g;n;s;alpha;delta];
%%%%% Declare some symbols
syms kk gg nn ss aa dd
answer = solve('(1+gg)*(1+nn)*kk-ss*kk^aa-(1-dd)*kk=0',kk);
k_bars = double(subs(answer,{gg,nn,ss,aa,dd},parameters))
% Matlab treats "symbols" differently to "numbers"; the commands above
% declare some symbols then solve the difference equation for its
% steady-state values. I then substitute the vector of parameters into the
% answer to get "numbers" back. Use the Matlab syntax "help syms", "help
% solve" etc etc to investigate this more closely.
% Keep the non-trival (positive) steady-state for future reference     
         
k_bar  = max(k_bars);
ky = k_bar^(1-alpha);
mpk = 1+alpha*k_bar^(alpha-1);
display(['Non-trivial steady state          =   ',num2str(k_bar)])
display(['Capital/output ratio              =   ',num2str(ky)])
display(['Gross marginal product of capital =   ',num2str(mpk)])
%%%%% QUESTION 2: TRANSITION TO STEADY STATE
error = 10;
iter  = 1;
k0    = k_bar*exp(-0.5);
% This sets the initial condition while the next command sets up an empty
% vector that we will use to store the sequence of capital stocks.
kt    = [];
% The following loop iterates on the difference equation until the error is
% small (less than 10e-6)
while error > 10e-6
    if iter == 1
        k = k0;
    else
        k = kt(iter-1);
    end
   
    k1 = (s/((1+g)*(1+n)))*k^(alpha) + ((1-delta)/((1+g)*(1+n)))*k;
% The previsious line computes the subsequent capital stock k1 given
% today's capital stock k
   
    error = abs(k1-k_bar);
    kt = [kt;k1];
    iter  = iter+1;  
   
end
T = length(kt);
t = [0;cumsum(ones(T-1,1))];
figure(1)
plot(t,kt,t,k_bar*ones(T,1),'k--')
title('Figure 1: Time path of the capital stock')
xlabel('time, t')
ylabel('capital stock, k(t)')
legend('k(t)',0)
%%%%% Now for the phase diagram (this is a little tricky...)
kmax = 2*k_bar;
ks   = linspace(0,kmax,1000);
psis = (s/((1+g)*(1+n)))*ks.^(alpha) + ((1-delta)/((1+g)*(1+n)))*ks;
kt0 = kt(1:T-1);
kt1 = kt(2:T);
figure(2)
if k0 < k_bar
    plot(kt0,kt1,'b>-',ks,ks,'k--',ks,psis,'r-')
    title('Figure 2: Phase diagram')
else
    plot(kt0,kt1,'b<-',ks,ks,'k--',ks,psis,'r-')
    title('Figure 2: Phase diagram')
end
xlabel('k(t)')
ylabel('k(t+1)')
legend('k(t+1)','45-degree line','\psi(k(t))',0)
%%%%% PROBLEM 3: GROWTH RATE APPROXIMATIONS
%%%%% The next command produces a 1000-by-1 vector of evenly spaced "zs"
%%%%% between 0 and 1.
zs = linspace(0,1,1000)';
g_approximate     = log(1+zs);
g_actuals         = zs;
figure(3)
plot(100*zs,100*g_approximate,'k--',100*zs,100*g_actuals)
title('Figure 3: Accuracy of log-differences as aproximate growth rates')
xlabel('growth rate (%)')
legend('log-approximate growth rate','actual growth rate',0)

运行后显示如下错误:
??? Attempt to execute SCRIPT solve as a function.
Error in ==> D:\Matlab\toolbox\symbolic\@sym\solve.m
On line 49  ==> [varargout{1:max(1,nargout)}] = solve(S{:});

Error in ==> D:\Matlab\work\ps0_solutions.m
On line 25  ==> answer = solve('(1+gg)*(1+nn)*kk-ss*kk^aa-(1-dd)*kk=0',kk);


谢谢!!!
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

关键词:MATLAB程序 MATLAB atlab matla 怎么修改 MATLAB 程序 高手 坛子

沙发
tulipsliu 在职认证  发表于 2011-4-16 12:42:46
没看明白;
解决什么问题的?
我先拿到程序里跑一边试试。
劳动经济学

藤椅
Xaero 发表于 2011-4-16 12:57:44
在你的当前目录,Matlab的搜索路径里面找 solve.m , 然后把除了Matlab系统里面自带的solve.m 都改名,然后看看
十年一觉扬州梦。
智不足以Academy,才尚不够Industry,[情无力于Life]。

板凳
cdconan 发表于 2011-4-16 13:00:27
16行solve函数找不到显示解,因此answer没能被赋值
可以试试fsolve求数值解

报纸
wb123456 发表于 2011-4-17 23:00:14
cdconan 发表于 2011-4-16 13:00
16行solve函数找不到显示解,因此answer没能被赋值
可以试试fsolve求数值解
哥们,试过了,不行啊~这个问题很纠结,还请大家帮忙啊

地板
qibbxxt 发表于 2011-4-18 11:07:12
5# wb123456

  1. %%%%% Matlab program (.m) file that gives solutions to "Problem Set Zero"
  2. %%%%% You should play with this file to learn simple Matlab commands.
  3. %%%%% Use "help" at the command window to figure out what various commands
  4. %%%%% do. Eg, type "help clear" to figure out what the next line does.
  5. clear all
  6. %%%%% QUESTION 1: STEADY STATE
  7. %%%%% Declare parameter values
  8. g     = 0.03;
  9. n     = 0.01;
  10. s     = 0.20;
  11. alpha = 0.33;
  12. delta = 0.04;
  13. parameters = [g;n;s;alpha;delta];
  14. %%%%% Declare some symbols
  15. % syms kk gg nn ss aa dd
  16. k_bars = fsolve(@(k)(1+g)*(1+n)*k-s*k^alpha-(1-delta)*k,4);
  17. % k_bars = double(subs(answer,{gg,nn,ss,aa,dd},parameters))
  18. % Matlab treats "symbols" differently to "numbers"; the commands above
  19. % declare some symbols then solve the difference equation for its
  20. % steady-state values. I then substitute the vector of parameters into the
  21. % answer to get "numbers" back. Use the Matlab syntax "help syms", "help
  22. % solve" etc etc to investigate this more closely.
  23. % Keep the non-trival (positive) steady-state for future reference     
  24.          
  25. k_bar  = max(k_bars);
  26. ky = k_bar^(1-alpha);
  27. mpk = 1+alpha*k_bar^(alpha-1);
  28. display(['Non-trivial steady state          =   ',num2str(k_bar)])
  29. display(['Capital/output ratio              =   ',num2str(ky)])
  30. display(['Gross marginal product of capital =   ',num2str(mpk)])
  31. %%%%% QUESTION 2: TRANSITION TO STEADY STATE
  32. error = 10;
  33. iter  = 1;
  34. k0    = k_bar*exp(-0.5);
  35. % This sets the initial condition while the next command sets up an empty
  36. % vector that we will use to store the sequence of capital stocks.
  37. kt    = [];
  38. % The following loop iterates on the difference equation until the error is
  39. % small (less than 10e-6)
  40. while error > 10e-6
  41.     if iter == 1
  42.         k = k0;
  43.     else
  44.         k = kt(iter-1);
  45.     end
  46.    
  47.     k1 = (s/((1+g)*(1+n)))*k^(alpha) + ((1-delta)/((1+g)*(1+n)))*k;
  48. % The previsious line computes the subsequent capital stock k1 given
  49. % today's capital stock k
  50.    
  51.     error = abs(k1-k_bar);
  52.     kt = [kt;k1];
  53.     iter  = iter+1;  
  54.    
  55. end
  56. T = length(kt);
  57. t = [0;cumsum(ones(T-1,1))];
  58. figure(1)
  59. plot(t,kt,t,k_bar*ones(T,1),'k--')
  60. title('Figure 1: Time path of the capital stock')
  61. xlabel('time, t')
  62. ylabel('capital stock, k(t)')
  63. legend('k(t)',0)
  64. %%%%% Now for the phase diagram (this is a little tricky...)
  65. kmax = 2*k_bar;
  66. ks   = linspace(0,kmax,1000);
  67. psis = (s/((1+g)*(1+n)))*ks.^(alpha) + ((1-delta)/((1+g)*(1+n)))*ks;
  68. kt0 = kt(1:T-1);
  69. kt1 = kt(2:T);
  70. figure(2)
  71. if k0 < k_bar
  72.     plot(kt0,kt1,'b>-',ks,ks,'k--',ks,psis,'r-')
  73.     title('Figure 2: Phase diagram')
  74. else
  75.     plot(kt0,kt1,'b<-',ks,ks,'k--',ks,psis,'r-')
  76.     title('Figure 2: Phase diagram')
  77. end
  78. xlabel('k(t)')
  79. ylabel('k(t+1)')
  80. legend('k(t+1)','45-degree line','\psi(k(t))',0)
  81. %%%%% PROBLEM 3: GROWTH RATE APPROXIMATIONS
  82. %%%%% The next command produces a 1000-by-1 vector of evenly spaced "zs"
  83. %%%%% between 0 and 1.
  84. zs = linspace(0,1,1000)';
  85. g_approximate     = log(1+zs);
  86. g_actuals         = zs;
  87. figure(3)
  88. plot(100*zs,100*g_approximate,'k--',100*zs,100*g_actuals)
  89. title('Figure 3: Accuracy of log-differences as aproximate growth rates')
  90. xlabel('growth rate (%)')
  91. legend('log-approximate growth rate','actual growth rate',0)
复制代码

pinggu.jpg (17.06 KB)

pinggu.jpg

pinggu1.jpg (19.43 KB)

pinggu1.jpg

pinggu2.jpg (24.35 KB)

pinggu2.jpg

7
wb123456 发表于 2011-4-18 18:56:37
谢谢上楼的朋友

8
tulipsliu 在职认证  发表于 2011-4-29 17:18:10
6楼很强啊。遇到几次了。
劳动经济学

9
tulipsliu 在职认证  发表于 2011-4-29 17:22:05
对了,你说这些程序是出自一本经济学的书籍。
可以推荐下不?
我找找看。
劳动经济学

10
matlab-007 发表于 2016-8-14 19:14:56
函数定义关键字是function,不是Function

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-5 14:28