Lagrange Interpolation singleton error

2 ビュー (過去 30 日間)
Mitul Dattani
Mitul Dattani 2019 年 3 月 6 日
回答済み: Vijaya Lakshmi Chagi 2019 年 3 月 13 日
Hi, I've been give code and have to add a line in to complete a Lagrange Interpolation, I googled it and found a couple threads tried them out but got the error saying:
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in cw1>my_interpolation (line 404)
L_k(k,:)=L_k(k,:).*(x-x(j))/(x(k)-x(j));
The code causing the error is:
function P = my_interpolation(x,y,N)
N_points = length(x); %the number of interpolation points from the input data
X = linspace(min(x),max(x),N); % creates a uniform vector of points from min(x) to max(x) with resolution N of the interpolating polynomial (not to be confused by the number of data points)
X=X'; % row vector to column vector
P = zeros(N,1); % creates an empty vector of zeros for interpolating polynomial
for k=1:N_points % loop over data points
L_k = ones(N,1); % defines initial Lagrange basis polynomial
for j=1:N_points % second loop over data points
if(k~=j) % conditional statement
%*************************** task 3 *************************************
% You need to complete the next line of code
% This line generates the Lagrange Basis Polynomials L_k used in the algorithm for the Lagrange Interpolating Polynomial
L_k(k,:)=L_k(k,:).*(x-x(j))/(x(k)-x(j));
%***************************** end of task 3 ***********************************
end
end
P = P + y(k)*L_k;
end
end
The line in between the comments is the one ive added. Any help is much appreciated even a push in the right direction.
Thanks
Mitul

回答 (1 件)

Vijaya Lakshmi Chagi
Vijaya Lakshmi Chagi 2019 年 3 月 13 日
Hi,
The error message occurs when you try to assign to a variable, but the indices of the left- and right-hand side of the assignment are incompatible. For example,
A(1) = [1 2 3];
throws this error because the size of the left-hand side is 1-by-1, but the size of the right-hand side is 1-by-3.
In your case, the RHS 'L_k(k,:).*(x-x(j))/(x(k)-x(j))' might be returning an array or vector which is not compatible with the LHS. Could you try storing the result of the RHS in a new varibale and check for its dimensions.

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by