How to solve Lagrange polynomial?
4 ビュー (過去 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!