HELP function y=lagrange (n,x,y) ↑ Error: Function definition not supported in this context. Create functions in code file.
Mostrar comentarios más antiguos
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 comentario
Walter Roberson
el 22 de Oct. de 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.
Respuestas (1)
madhan ravi
el 22 de Oct. de 2018
Editada: madhan ravi
el 22 de Oct. de 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 comentarios
Walter Roberson
el 22 de Oct. de 2018
Note: this requires R2016b or later if those are all in the same file.
madhan ravi
el 22 de Oct. de 2018
Thank you sir Walter Edited
madhan ravi
el 23 de Oct. de 2018
@Ramirez did you try the answer?
Categorías
Más información sobre Whos en Centro de ayuda y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!