Finding Coefficients in Bspline is making a problem ?

2 次查看(过去 30 天)
% Define the x array
N = 100; % Example N value, adjust as needed
x = linspace(0, 5, N + 2); % Example x values, adjust as needed
% Initialize the a array
a = zeros(1, N + 2);
% Compute the coefficients a_i
for i = 1:(N + 2)
product = 1;
for j = 1:(N + 2)
if j ~= i
product = product * (x(i) - x(j));
end
end
a(i) = 1 / product;
end
% Display the coefficients
disp('Coefficients a_i:');
disp(a);
This is my code but the values of this coefficient are coming in negative is my code right?THIS IS THE ACTUAL FORMULA TO SOLVE

回答(1 个)

sai charan sampara
sai charan sampara 2024-7-17
Hello Rohitasya,
The code you have written is accuarte for calculating the coeffecients.The following code achieves the same result as yours:
N = 100;
x = linspace(0, 5, N + 2);
b = zeros(1, N + 2);
for i=1:(N+2)
x_new=x(i)-x;
x_new(i)=1;
product=prod(x_new);
b(i)=1/product;
end
disp(b)
Columns 1 through 18 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 Columns 19 through 36 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0001 0.0002 -0.0005 0.0013 -0.0031 0.0070 -0.0154 0.0322 -0.0643 0.1231 Columns 37 through 54 -0.2257 0.3966 -0.6679 1.0790 -1.6724 2.4882 -3.5546 4.8772 -6.4290 8.1434 -9.9137 11.6012 -13.0513 14.1168 -14.6814 14.6814 -14.1168 13.0513 Columns 55 through 72 -11.6012 9.9137 -8.1434 6.4290 -4.8772 3.5546 -2.4882 1.6724 -1.0790 0.6679 -0.3966 0.2257 -0.1231 0.0643 -0.0322 0.0154 -0.0070 0.0031 Columns 73 through 90 -0.0013 0.0005 -0.0002 0.0001 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 Columns 91 through 102 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000 -0.0000 0.0000
Since "x" is an array of increasing values,for any iteration "i", the values of "x_new" in index positions 1 to "i-1" will be posiitve, at "i" the value of "x_new" is 1 and for index "i+1" to "N+2" the values are negative. So when calculating the product you are esssentially taking the product of "i" positive numbers and "N+2-i" negative numbers. So for even values of "i" the product is positive and for odd values it is negative. Hence the array "a" in your case has alternate negative and positive values. The same is shown in the code below:
for i=1:(N+2)
x_new=x(i)-x;
x_new(i)=1;
product=prod(x_new);
b(i)=1/product;
if(i==25 ||i==26)
disp("Value of i: "+i)
x_new
disp("Number of negative terms: "+sum(x_new<0))
disp("Product of the terms: "+product)
end
end
Value of i: 25
x_new = 1x102
1.1881 1.1386 1.0891 1.0396 0.9901 0.9406 0.8911 0.8416 0.7921 0.7426 0.6931 0.6436 0.5941 0.5446 0.4950 0.4455 0.3960 0.3465 0.2970 0.2475 0.1980 0.1485 0.0990 0.0495 1.0000 -0.0495 -0.0990 -0.1485 -0.1980 -0.2475
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Number of negative terms: 77
Product of the terms: -130056.8999
Value of i: 26
x_new = 1x102
1.2376 1.1881 1.1386 1.0891 1.0396 0.9901 0.9406 0.8911 0.8416 0.7921 0.7426 0.6931 0.6436 0.5941 0.5446 0.4950 0.4455 0.3960 0.3465 0.2970 0.2475 0.1980 0.1485 0.0990 0.0495 1.0000 -0.0495 -0.0990 -0.1485 -0.1980
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Number of negative terms: 76
Product of the terms: 42226.2662
If you are expecting the coefficients to be of different behaviour then it is purely based on the data "x".

类别

Help CenterFile Exchange 中查找有关 Splines 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by