简体   繁体   English

在 Matlab 函数中使用方程的变换?

[英]Use transformation of an equation within a Matlab function?

I am trying to code a very basic Mean Likelihood Estimator function that relies on the Optimization Toolbox's fminsearch() function.我正在尝试编写一个非常基本的平均似然估计器函数,该函数依赖于优化工具箱的fminsearch()函数。 I try to pass a function handle as an input and optimize the sum of the neg.我尝试将函数句柄作为输入传递并优化否定的总和。 of the function's log.函数的日志。 My code is:我的代码是:

function [ params, max ] = routine( fun )
%UNTITLED3 Summary of this function goes here
%   Detailed explanation goes here

[filename,path] = uigetfile('*.'); #To grab the csv file location
name = strcat(path,filename);
data = csvread(name);

lh = @(x) sum(-log( fun(x) )); <-------- ERROR LINE

options = optimset('Display', 'off', 'MaxIter', 100000, 'TolX', 10^-20, 'TolFun', 10^-20);
[theta, max1] = fminsearch(lh, [0,1], options);

params = theta
max = max1

end

The line in the middle is giving me this error:中间的那一行给了我这个错误:

Undefined function or variable 'data'.

Error in @(x)(1/(sqrt(2*pi)*x(2)))*exp((-(data-x(1)).^2)/(2*x(2)^2))

Error in @(x)sum(-log(fun(x)),data)

Error in fminsearch (line 200)
fv(:,1) = funfcn(x,varargin{:});

Now, outside this code, this works perfectly fine, infuriatingly enough.现在,在这段代码之外,这工作得非常好,令人气愤。

pan = @(x)sum(-log((1/(sqrt(2*pi)*x(2)))*exp((-(data-x(1)).^2)/(2*x(2)^2))));
options = optimset('Display', 'off', 'MaxIter', 100000, 'TolX', 10^-20, 'TolFun', 10^-20);
[theta, max1] = fminsearch(pan, [0,1], options);

For some reason, transforming the equation the way I do in the middle makes the data var.出于某种原因,按照我在中间所做的方式转换方程会使data invisible.看不见。 How should I correctly apply the transformation to my equation such that this code works?我应该如何正确地将转换应用到我的方程以使此代码有效?

Take a look at the documentation on anonymous functions.查看匿名函数的文档 Here's the relevant part这是相关的部分

Variables in the Expression表达式中的变量

Function handles can store not only an expression, but also variables that the expression requires for evaluation.函数句柄不仅可以存储表达式,还可以存储表达式求值所需的变量。

For example, create a function handle to an anonymous function that requires coefficients a , b , and c .例如,为需要系数abc的匿名函数创建函数句柄。

 a = 1.3; b = .2; c = 30; parabola = @(x) a*x.^2 + b*x + c;

Because a , b , and c are available at the time you create parabola , the function handle includes those values.由于abc在您创建parabola时可用,因此函数句柄包含这些值。 The values persist within the function handle even if you clear the variables:即使您清除了变量,这些值仍然存在于函数句柄中

 clear abc x = 1; y = parabola(x)

y = 31.5000 y = 31.5000

To supply different values for the coefficients, you must create a new function handle要为系数提供不同的值,您必须创建一个新的函数句柄

In your case the value of data that you read in won't be available to fun since the function handle was defined before data .你的情况的值data您阅读将无法使用fun ,因为函数句柄之前定义的data Instead we can just make data a parameter of fun then we avoid the problem all together.相反,我们可以让data成为fun的参数,然后我们一起避免这个问题。


calling code调用代码

myfun = @(x,data) (1/(sqrt(2*pi)*x(2)))*exp((-(data-x(1)).^2)/(2*x(2)^2));
[params,max] = routine(myfun);

routine.m (the bit which needs to be changed)例程.m (需要更改的位)

function [ params, max ] = routine( fun )
...
lh = @(x) sum(-log( fun(x,data) ));
...
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM