I want to find and display the polynomial equation for degree 1 and 2 using Newton Forward Divided Difference but the system deos not recognize when n == 3.
조회 수: 1 (최근 30일)
이전 댓글 표시
Supposedly when n = 3, the system will produce a polynomial equation for degree 2. the answer should be 0.5552 x^2 - 2.4446x + 3.6889.
댓글 수: 0
답변 (1개)
Subhajyoti
2024년 7월 15일
Hi Azneen,
The “size” function returns a tuple of number of rows and columns present in the input data-matrix.
% Sample Data Table
c = [1.6, 1.2;
1.9, 1.05;
2.2, 1.0
]
n = size(c)
Now, when ‘n’ is compared with any scalar value, it performs an element wise comparison and return a Boolean vector (say, ‘b’). When ‘b’ is used in if-else conditional, it uses ‘all(b)’ to get a scalar Boolean value.
n == 3
Here’s MATLAB code snippet to illustrate using ‘size’ function for data-matrix:
% Sample Data Table
c = [1.6, 1.2;
1.9, 1.05;
2.2, 1.0
];
[n, ~] = size(c)
value = '';
if n==2
D1 = (c(2,2) - c(1,2)) / (c(2,1) - c(1,1));
a = D1;
b = c(1,2) - (D1*c(1,1));
value = sprintf('%.2f %+-.2f*x', b, a);
elseif n==3
D1 = (c(2,2) - c(2,1)) / (c(2,1) - c(1,1));
D2 = (c(3,2) - c(2,2)) / (c(3,1) - c(2,1));
D3 = (D2 - D1) / (c(3,1) - c(2,1));
a = D3;
b = D1 - D3*(c(2,1)+c(1,1));
c = (D3*c(1,1)*c(2,1))- D1*c(1,1) + c(1,2);
value = sprintf('%.2f %+-.2f*x %+-.2f*x^2', c, b, a);
else
value = 'THIS SYSTEM CAN ONLY FIND POLYNOMIALS DEGREE 1 AND 2 ONLY';
end
value
Here, we are explicitly saving the first value of the tuple (number of rows in the data-matrix) into ‘n’. This gives us the desired conditional in the switch-cases.
For more details on the “size,” you can go through the following MathWorks documentation.
Hope the above information is helpful.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Polynomials에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!