matlab code for lagrange methode

11 ビュー (過去 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 Center および File ExchangeCreating and Concatenating Matrices についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by