【问题标题】:Issues defining functions in Matlab在 Matlab 中定义函数的问题
【发布时间】:2014-04-18 03:13:15
【问题描述】:

我在 Matlab 的第一个晚上遇到了一些麻烦。我猜我是在 R 中获得桂冠......

我正在尝试解决一个欧拉方程的无限时间平衡问题。

Matlab 似乎更适合在这个问题中进行优化,但我刚开始定义函数时遇到了一些问题。这是我所拥有的:

%% ECON 20210 Honors Problem Set #2 Code
%% Writen By Jacob Miller
%% Date 4/17/2014

%% In this problem, you have to write 
%% a program to solve the following social planner problem:
%% Maximize The sum from t=1 to T over Ct,Nt, Kt+1 of the function
%% sum from t=1 to T of B^t-1*(Ct^(1-y)/1-y)-psi*log(n) 
%% subject to the constraint Ct=A(kt^a)*(nt^1-a)+(1-d)Kt-Kt+1

function [eul] = funkone(c,y,p,n)
eul = (power(c,minus(1,y))/minus(1,y)-p*log(n));

现在正在返回错误:

EDU>> MillerEconPset2
Error using MillerEconPset2 (line 12)
Not enough input arguments.

似乎认为我正在尝试运行该函数,但我只想定义它。我在 R 中有一个类似的脚本:

myfunc<-function(c,y,n,p){
  (c^(1-y))/(1-y)-n*log(p)
}

但不幸的是,R 在求解全局均衡方面几乎没有那么好。知道如何修复 Matlab 代码吗?

感谢十亿。

最好的,

杰克

【问题讨论】:

  • MillerEconPset2 是脚本文件吗?如果可以,我们可以查看吗?

标签: r matlab function maximize


【解决方案1】:

通过创建一个包含函数的文件并使用与函数名称匹配的适当文件名保存它来定义函数。独立函数应在与主脚本不同的文件中定义。然后,您将能够访问以这种方式定义的任何函数,只要它们位于您的 MATLAB 搜索路径或当前工作目录中。

在这种情况下可行的替代方法是使用匿名函数。那就是:

funkone = @(c,y,p,n) (power(c,minus(1,y))/minus(1,y)-p*log(n));

可以称为

ouput_val = funkone(c,y,p,n)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多