필터 지우기
필터 지우기

To reduce the number of lines

조회 수: 2 (최근 30일)
SREERAJ WARRIER
SREERAJ WARRIER 2021년 9월 25일
댓글: Sambit Supriya Dash 2021년 9월 25일
This is a code for interpolation. I would like to minimize the code lines by using summation and product. Can anyone help me?
%% Interpolation
clear all
x = [-2 -1 1 3];
y = [-1 3 -1 19];
syms t;
syms l1 l2 l3 l4;
l1 = ((t-x(2))*(t-x(3))*(t-x(4)))/((x(1)-x(2))*(x(1)-x(3))*(x(1)-x(4)));
l2 = ((t-x(3))*(t-x(4))*(t-x(1)))/((x(2)-x(3))*(x(2)-x(4))*(x(2)-x(1)));
l3 = ((t-x(4))*(t-x(1))*(t-x(2)))/((x(3)-x(4))*(x(3)-x(1))*(x(3)-x(2)));
l4 = ((t-x(1))*(t-x(2))*(t-x(3)))/((x(4)-x(1))*(x(4)-x(2))*(x(4)-x(3)));
PnX = y(1)*l1 + y(2)*l2 + y(3)*l3 + y(4)*l4
Px = simplify(PnX)
figure; fplot(Px)

답변 (1개)

Sambit Supriya Dash
Sambit Supriya Dash 2021년 9월 25일
This may help you, in terms of looping, addition and substraction,
x = [-2 -1 1 3];
y = [-1 3 -1 19];
syms t;
for i = 1:length(x)
Num = prod(t-x([1:(i-1) (i+1):end]));
Den = prod(x(i)-x([1:(i-1) (i+1):end]));
l{i} = Num/Den;
end
for j = 1:length(y)
p(j) = y(j)*l{i};
end
PnX = sum(p);
Px = simplify(PnX)
figure; fplot(Px)
  댓글 수: 2
SREERAJ WARRIER
SREERAJ WARRIER 2021년 9월 25일
The figures are different for both code. The y-limit is different
Sambit Supriya Dash
Sambit Supriya Dash 2021년 9월 25일
l is same for both the cases I guess.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by