필터 지우기
필터 지우기

Possible to "expand" function handle?

조회 수: 1 (최근 30일)
dm
dm 2012년 2월 18일
I've written a function that returns a function handle to a Lagrange polynomial L(x):
function L = lagrange(xv,yv)
% make sure xv and yv are row vectors
xv = xv(:);
yv = yv(:);
% check for equal dimensions
if size(xv) ~= size(yv)
error('xv and yv must be of same dimensions');
end
% number of points
n = length(xv);
% initialize to zero
L = @(x)0;
for k = 1:n
l = @(x)1; % initialize k'th basis polynomial
for m = 1:n
if k ~= m % if k different from m, calculate basis
l = @(x)(l(x).*(x-xv(m))/(xv(k)-xv(m)));
end
end
L = @(x)(L(x)+yv(k).*l(x)); % perform linear combination
end
It seems to work as I want it to (calling L(4), or whatever, produces the correct result), but if I type L in the command line, Matlab prints "L = @(x)(L(x)+yv(k).*l(x))".
Is it possible to get the complete function printed out? I.e., if my yv vector stems from the function x^3, then the Lagrange polynomial should be 6x^2-11x+6, but I can't figure out how to get this printed out; is it posible?
best regards
dm

채택된 답변

Walter Roberson
Walter Roberson 2012년 2월 18일
Sorry, no it is not.
  댓글 수: 2
dm
dm 2012년 2월 18일
Oh well. Not a big deal, was just curious if there could be something that I had overlooked.
Walter Roberson
Walter Roberson 2012년 2월 19일
You may wish to consider using symbolic expressions instead.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by