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.

回答 (1 件)

Subhajyoti
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
]
c = 3x2
1.6000 1.2000 1.9000 1.0500 2.2000 1.0000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
n = size(c)
n = 1x2
3 2
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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
ans = 1x2 logical array
1 0
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)
n = 3
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
value = '32.76 -33.94*x +8.89*x^2'
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.

カテゴリ

Help Center および File ExchangePolynomials についてさらに検索

製品


リリース

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by