HELP function y=lagrange (n,x,y) ↑ Error: Function definition not supported in this context. Create functions in code file.
Show older comments
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 Comment
Walter Roberson
on 22 Oct 2018
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.
Answers (1)
madhan ravi
on 22 Oct 2018
Edited: madhan ravi
on 22 Oct 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
3 Comments
Walter Roberson
on 22 Oct 2018
Note: this requires R2016b or later if those are all in the same file.
madhan ravi
on 22 Oct 2018
Thank you sir Walter Edited
madhan ravi
on 23 Oct 2018
@Ramirez did you try the answer?
Categories
Find more on Whos in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!