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
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
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
2018년 10월 22일
Note: this requires R2016b or later if those are all in the same file.
madhan ravi
2018년 10월 22일
Thank you sir Walter Edited
madhan ravi
2018년 10월 23일
@Ramirez did you try the answer?
카테고리
도움말 센터 및 File Exchange에서 Whos에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!