I am trying to use minimization to calculate coefficients of the polynomial p(x) = 1 + c(1)x + c(2)x^2
to approximate e^x
. I need to use points xi = 1 + i/n
for natural numbers i
on [1,n]
, first for n=5
, then n=10
, etc. The approach is to minimize the 1
, 2
, and inf norm(p(x) - e^x)
using fminsearch. So the output should be the 2 coefficients for the 3 p(x)
's. Any suggestions is appreciated.
Asked
Active
Viewed 656 times
0

lennon310
- 12,503
- 11
- 43
- 61

LuckyPenny
- 39
- 6
-
Did you try anything yourself? What happened? – David Oct 31 '14 at 03:43
-
@David Yes, I finally figured it out. – LuckyPenny Nov 03 '14 at 18:56
1 Answers
0
Well, if anyone was wondering, I did figure the question out finally.
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
This worked in the end to answer the question.

LuckyPenny
- 39
- 6