How to solve Lagrange polynomial?

6 Ansichten (letzte 30 Tage)
Tariq Saleh
Tariq Saleh am 15 Feb. 2022
Beantwortet: Walter Roberson am 15 Feb. 2022
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))
3 2 - 10 x + 122 x - 448 x + 516

Antworten (1)

Walter Roberson
Walter Roberson am 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.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by