matlab code for lagrange methode

조회 수: 12 (최근 30일)
Razan
Razan 2020년 4월 7일
답변: Nagasai Bharat 2020년 8월 25일
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
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
darova 2020년 4월 7일
I have a problem
Error: Values of x exceeds the number of node points

댓글을 달려면 로그인하십시오.

답변 (1개)

Nagasai Bharat
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

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

Community Treasure Hunt

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

Start Hunting!

Translated by