2014-10-30 114 views
0

我正在尝试使用最小化来计算多项式的系数p(x) = 1 + c(1)x + c(2)x^2以近似e^x。我需要使用点xi = 1 + i/n为自然数i[1,n],首先为n=5,然后n=10等的方法是12inf norm(p(x) - e^x)使用fminsearch的最小化。所以输出应该是3 p(x)的2个系数。任何建议表示赞赏。Matlab最小化范数以使用fminsearch查找系数

+0

你有什么尝试吗?发生了什么? – David 2014-10-31 03:43:28

+0

@大卫是的,我终于明白了。 – LuckyPenny 2014-11-03 18:56:26

回答

0

好吧,如果有人想知道,我最终确定了这个问题。

for l = [1 2 inf] 
    fprintf('For norm %d\n', l) 
    fprintf('Coefficients c1  c2\n') 
    for n = [5 10 100] 
     i = 1:n ; 
     x = 1 + i/n ; 
     c = [1 1] ; 

     %Difference function that we want to minimize 
     g = @(c) x.*c(1) + x.^2.*c(2) + 1 - exp(x); 
     f_norm = @(c) norm(g(c), l) ; 
     C = fminsearch(f_norm, c); 
     fprintf('n = %d  ', n) 
     fprintf('%f %f\n', C(1), C(2)) 
     % Compare plot of e^x and p(x). 
     p = @(x) C(1)*x + C(2)*x.^2 + 1; 
     xx = linspace(1,2,1e5); 
     figure; 
     plot(xx, p(xx), '--r', xx, exp(xx)); 
     str = sprintf('Plot with n = %d, for norm %d', n,l); 
     title(str,'FontSize',24) 
     xlabel('x','FontSize',20) 
     ylabel('y','FontSize',20) 
     legend('p2 approximation','exponential'); 
    end 
end 

这工作到最后回答这个问题。