HELP function y=lagrange (n,x,y) ↑ Error: Function definition not supported in this context. Create functions in code file.

function y=lagrange (n,x,y)
↑
Error: Function definition not supported in this context. Create
functions in code file.
this is my code:
function y=lagrange(n,x,y)
p=size(x,2);
H=ones(p,size(n,2));
if (size(x,2)~=size(y,2))
fprintf(1,'\nERROR…\n x and y must contain equal no. of elements\n');
y=NaN;
else
for i=1:p
for j=1:p
if (i~=j)
L(i,:)=L(i,:).*(n-x(j))/(x(i)-x(j));
end
end
end
y=0;
for i=1:p
y=y+y(i)*L(i,:);
end
end

1 Kommentar

You should store the code in a file named lagrange.m
It is not possible to create functions at the command prompt.
In R2016b and later, it is possible to create functions inside a script file, but not in R2016a or earlier. I think the error message was different when you tried in R2016a or earlier.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

madhan ravi
madhan ravi am 22 Okt. 2018
Bearbeitet: madhan ravi am 22 Okt. 2018
%SCRIPT FILE
x = yourdata
y = yourdata
n = your_data
Y=lagrange(n,x,y) %function calling
%FUNCTION FILE
function y=lagrange(n,x,y)
p=size(x,2);
H=ones(p,size(n,2));
if (size(x,2)~=size(y,2))
fprintf(1,'\nERROR…\n x and y must contain equal no. of elements\n');
y=NaN;
else
for i=1:p
for j=1:p
if (i~=j)
L(i,:)=L(i,:).*(n-x(j))/(x(i)-x(j));
end
end
end
y=0;
for i=1:p
y=y+y(i)*L(i,:);
end
end

Kategorien

Mehr zu MATLAB finden Sie in Hilfe-Center und File Exchange

Produkte

Gefragt:

am 22 Okt. 2018

Kommentiert:

am 23 Okt. 2018

Community Treasure Hunt

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

Start Hunting!

Translated by