How to solve Lagrange polynomial?
4 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Walter Roberson
on 15 Feb 2022
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 Comments
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!