[英]Use recursive MATLAB function or optimization?
我正在嘗試確定一種最有效,最精確的方法來計算一個稱為R
的值,該值只能取0
到1
之間的值。 現在,我在下面的腳本中使用了下面的函數,但是我感覺我以一種非最佳的方式進行操作。 目前,我得到一個答案,並且必須(再次)將該答案作為初始“猜測”,以獲取(下一個)最佳答案。 我可以為此建立更好的遞歸,還是可以使用Matlab的求解器之一? 謝謝!
功能:
function f = Rfind(p,u,R)
f = p .* (R.^u);
end
劇本:
R = 0.999995753651217; % initial guess
matches = false;
while ~matches && R < 1
R = R + 0.0000000000000000001; % increment R for next guess
Jtotal = sum(Rfind(p,u,R)); % find R
if abs(Jtotal - R)*10000000000 < 5 % check precision of result
matches = true; % if R matches R fed to function, successful
end
end
Jtotal
我要確定的是:
求出R
的值等於數組p
乘以R
到數組u
的冪的和。 數組p
和數組u
都具有相同數量的元素,即,每1列包含12行。 我的函數為每個p
和u
行計算R
,然后增加其猜測值以找到下一個最接近的匹配項。 一旦達到精度極限或輸入R
和輸出總和相同,它將停止。
示例數據:
數組p
0.00000693
0.00000231
0.00001386
0.00000924
0.00041360
0.00461657
0.03085337
0.01595235
0.09614154
0.06832660
0.11103563
0.67262800
數組u
50000
500
50
25
10
7.5
5
3.5
2.5
1.25
1
0
重要提示 :為此,我需要最高的精度,但我不希望像上面的擴展那樣花費10分鍾。
您可以為此使用fminbnd
:
% first assign p and u
% define the function that you want to minimize:
Rfind = @(R) abs(sum(p.*(R.^u)) - R)
% set the tolerance to maximum:
options = optimset('TolX',eps);
% find the value between 0 to 1 that minimize the function Rfind:
[R, err] = fminbnd(Rfind,0,1,options)
並獲得(不到一秒鍾的時間):
R =
0.999995761369809
err =
9.196743366857163e-11
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.