从R的optim函数获取更多详细信息

编程入门 行业动态 更新时间:2024-10-11 09:25:47
本文介绍了从R的optim函数获取更多详细信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我对optim函数不是很熟悉,我想从其结果中获取这些信息:a)要获得结果,需要进行多少次迭代? b)绘制部分解的序列,即在每次迭代结束时获得的解.

I'm not very familiar with the optim function, and I wanted to get these informations from its results: a) how many iterations were needed for achieving the result? and b) to plot the sequence of partial solutions, that is, the solution obtained in the end of each iteration.

到目前为止,我的代码如下:

My code until now looks like this:

f1 <- function(x) { x1 <- x[1] x2 <- x[2] x1^2 + 3*x2^2 } res <- optim(c(1,1), f1, method="CG")

如何改进它以获得更多信息?

How can I improve it to get further information?

预先感谢

推荐答案

您可以修改函数以将传递给它的值存储到全局列表中.

You could modify your function to store the values that are passed into it into a global list.

i <- 0 vals <- list() f1 <- function(x) { i <<- i+1 vals[[i]] <<- x x1 <- x[1] x2 <- x[2] x1^2 + 3*x2^2 } res <- optim(c(1,1), f1, method="CG")

现在,如果您在运行函数后检查i和val,可以看到发生了什么.如果您想在optim运行时查看这些值,则也向该函数中添加一条print语句.

Now if you examine i and vals after you run the function you can see what happened. If you want to see the values while optim is running throw a print statement into the function as well.

更多推荐

从R的optim函数获取更多详细信息

本文发布于:2023-11-08 06:15:24,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1568567.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   更多详细   信息   optim

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!