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!