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 件のコメント

Walter Roberson
Walter Roberson 2018 年 10 月 22 日
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.

サインインしてコメントする。

回答 (1 件)

madhan ravi
madhan ravi 2018 年 10 月 22 日
編集済み: madhan ravi 2018 年 10 月 22 日
%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

3 件のコメント

Walter Roberson
Walter Roberson 2018 年 10 月 22 日
Note: this requires R2016b or later if those are all in the same file.
madhan ravi
madhan ravi 2018 年 10 月 22 日
Thank you sir Walter Edited
madhan ravi
madhan ravi 2018 年 10 月 23 日
@Ramirez did you try the answer?

サインインしてコメントする。

カテゴリ

製品

質問済み:

2018 年 10 月 22 日

コメント済み:

2018 年 10 月 23 日

Community Treasure Hunt

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

Start Hunting!

Translated by