matlab code for lagrange methode
12 次查看(过去 30 天)
显示 更早的评论
i getting this error :
Operator '-' is not supported for operands of type 'cell'.
Error in untitled (line 20)
code
clear all;
close all;
clc;
%%
syms x;
%Data
%Input data
n=input('Enter no. of node points:'); %no. of node points
%enter x values
a=input('Enter the values of x in braces:');
if length(a)==n
b=input('Enter the values of y in braces:');
%calculation of coeffiecients
for i=1:n
for j=1:n
c.c(i,j)=(a(1,i)-a(1,j));
end
end
c=c.c;
q=size(c);
c=c';
c(c==0)=[]; %deleting the zero values from the matrix
c=reshape(c,q(2)-1,q(1));
c=c';
j=1;
for i=1:n
d.d(i,j)=prod(c(i,:));
end
d=d.d;
d=d';
for i=1:n
coff.coff(i,j)=b(1,i)./d(1,i);
end
coff=coff.coff;
coff=coff'; %coefficients
%%
for i=1:n
for j=1:n
p.p(i,j)=(x-a(1,j));
end
end
p=p.p;
m=size(p);
p=p';
p(1:n+1:end)=[]; %deleting the diagonal elements
p=reshape(p,m(2)-1,m(1));
%%
p=prod(p);
pol=coff.*p;
pol=sum(pol); %Lagrange polynomial
%%
l=input('Enter a value to compute the b value:');
output=subs(pol,l);
%%
%Displaying output
disp('');
fprintf('Value of input x:=%f\n',double(output));
%%
if l>max(a)
x=min(a):0.1:l;
y=subs(pol,x);
plot(x,y,'r');
xlabel('X')
ylabel('Y')
else
x=min(a):0.1:max(a);
y=subs(pol,x);
plot(x,y,'r');
xlabel('X')
ylabel('Y')
end
else
display('Error: Values of x exceeds the number of node points')
end
%End of code
2 个评论
madhan ravi
2020-4-7
That’s why preallocation is really important. Preallocate variable before the loop to avoid ambiguities. You’re creating struct inside loops.
darova
2020-4-7
I have a problem
Error: Values of x exceeds the number of node points
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/282680/image.png)
回答(1 个)
Nagasai Bharat
2020-8-25
Hi Razan,
It is my understanding that you are performing some numerical computation using a cell array taken as input. To access an element of type cell array, curly brackets ‘{}’ to be used instead to parenthesis ‘()’. Or the cell array should be converted to matrices using cell2mat function.
Changes to your code as below should work.
if length(a)==n
b=input('Enter the values of y in braces:');
%calculation of coeffiecients
for i=1:n
for j=1:n
c.c(i,j)=(a{1,i}-a{1,j});
end
end
c=c.c;
q=size(c);
c=c';
c(c==0)=[]; %deleting the zero values from the matrix
c=reshape(c,q(2)-1,q(1));
c=c';
j=1;
for i=1:n
d.d(i,j)=prod(c(i,:));
end
d=d.d;
d=d';
for i=1:n
coff.coff(i,j)=b{1,i}./d(1,i);
end
coff=coff.coff;
coff=coff'; %coefficients
%%
for i=1:n
for j=1:n
p.p(i,j)=(x-a{1,j});
end
end
p=p.p;
m=size(p);
p=p';
p(1:n+1:end)=[]; %deleting the diagonal elements
p=reshape(p,m(2)-1,m(1));
%%
p=prod(p);
pol=coff.*p;
pol=sum(pol); %Lagrange polynomial
%%
l=input('Enter a value to compute the b value:');
output=subs(pol,l);
%%
%Displaying output
disp('');
fprintf('Value of input x:=%f\n',double(output));
%%
if l>max(cell2mat(a))
x=min(cell2mat(a)):0.1:l;
y=subs(pol,x);
plot(x,y,'r');
xlabel('X')
ylabel('Y')
else
x=min(cell2mat(a)):0.1:max(cell2mat(a));
y=subs(pol,x);
plot(x,y,'r');
xlabel('X')
ylabel('Y')
end
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!