2014-10-28 45 views
0

我有方程F(f)=a*f^3+b*f+c。我已經知道數據的向量,p,自變量'f'。我需要找到值a,b,c。 我試了一下:使用MATLAB在等式中查找常量值

function [ val ] = myfunc(par_fit,f,p) 
    % This gives me a,b,c 
    % p= af^3 +bf +c 
    val = norm(p - (par_fit(1)*(f.^3))+ (par_fit(2)*f) + (par_fit(3))); 
end 
my_par = fminsearch(@(par_fit) myfunc(par_fit,f,p),rand(1,3)); 

這讓我my_par = [1.9808 -2.2170 -24.8039],或者a=1.9808b=-2.2170c=-24.8039,但我需要b應大於5,和c應大於零。

+0

'c'不是零,它是-24.8039。有什麼問題? – David 2014-10-28 02:57:32

+0

對不起,我編輯過,c不能小於或等於零。 – Biparite 2014-10-28 03:01:02

+0

嘗試'val = norm(p - par_fit(1)* f。^ 3 - par_fit(2)* f - par_fit(3));'當您正在爲'p - a * f^3 - b * f - c' – Cheery 2014-10-28 03:07:51

回答

1

我認爲你的問題可能是因爲你的目標函數是不正確的:

val = norm(p - (par_fit(1)*(f.^3))+ (par_fit(2)*f) + (par_fit(3))); 

也許應該是:

val = norm(p-(par_fit(1)*f.^3+par_fit(2)*f+par_fit(3))); 

但是當你使用fmincon做到最小化,你可以約束變量的值而不是fminsearch。通過將lb輸入設置爲[-Inf -Inf 0],前兩個係數允許爲任何實數,但第三個係數必須大於或等於零。例如:(我也展示瞭如何使用矩陣方法解決問題(沒有非負性約束))

% Sample data 
f=(0:.1:1).'; 
p=2*f.^3+3*f+1+randn(size(f)) 

% Create Van der Monde matrix 
M=[f.^3 f f.^0]; 
C=M\p; % Solve the matrix problem in a least squares sense if size(f)>size(F) 

my_par=fmincon(@(c) norm(p-(c(1)*f.^3+c(2)*f+c(3))),rand(1,3),[],[],[],[],[-Inf 5 0],[]) 
C.' 

plot(f,p,'o',f,M*C,f,my_par(1)*f.^3+my_par(2)*f+my_par(3)) 
+0

感謝David的詳細解釋。在這種情況下,我希望b大於5,所以我應該改變什麼 – Biparite 2014-10-28 03:27:51