How to solve Lagrange polynomial?
조회 수: 6 (최근 30일)
이전 댓글 표시
I'm trying to find the function of these data set , but the output is wrong
what's wrong with this code?
syms x
x0=1;
x1=2;
x2=3;
x3=4;
y0=3;
y1=5;
y2=7;
y3=9;
L(x0)=(x-x1)*(x-x2)*(x-x3)/(x0-x1)*(x0-x2)*(x0-x3);
L(x1)=(x-x0)*(x-x2)*(x-x3)/(x1-x0)*(x1-x2)*(x1-x3);
L(x2)=(x-x0)*(x-x1)*(x-x3)/(x2-x0)*(x2-x1)*(x2-x3);
L(x3)=(x-x0)*(x-x1)*(x-x2)/(x3-x0)*(x3-x1)*(x3-x2);
y=L(x0)*(y1)+L(x1)*(y2)+L(x3)*(y3);
simplify(y);
pretty(simplify(y))
댓글 수: 0
답변 (1개)
Walter Roberson
2022년 2월 15일
Those assignments to L(x0) and so on are not defining the meaning of L when given an input variable named x0, and a different meaning if the input is x1 and so on. Instead each of those is overwriting all of the symbolic function named L.
MATLAB does not permit you to define a symbolic function by cases. You cannot, for example, define
f(0) = 1
f(t) = t*f(t-1)
in order to define the factorial function. There are computer programming languages that permit that kind of definition by cases, but matlab is not one of them. You need to either define a single piecewise() function that covers the four cases, or else you need to define four different functions.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Linear Algebra에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!