How can I create a function that returns a function?

17 ビュー (過去 30 日間)
Vitor Pessoa
Vitor Pessoa 2016 年 8 月 14 日
回答済み: Walter Roberson 2016 年 8 月 14 日
I'm tryng to create a function that returns to me the interpolation polynom of newton. This is the code:
function Y=newton(x,y)
syms X;
n=length(x);
b=zeros(n,n);
b(:,1)=y(:);
%finite difference matrix
for j=2:n
for i=1:n-j+1
b(i,j)= (b(i+1,j-1)-b(i,j-1))/(x(i+j-1)-x(i));
end
end
%interpolation polynom
yint=b(1,1);
xt=1;
for j=1:n-1
xt=xt*(X-x(j));
yint=yint+b(1,j+1)*xt;
end
Y=yint
It's already working, but I want the output to be a function, so I can calculate it's value in any point, make graphics, etc.
  2 件のコメント
Vitor Pessoa
Vitor Pessoa 2016 年 8 月 14 日
編集済み: Geoff Hayes 2016 年 8 月 14 日
function Y=newton(x,y)
syms X;
n=length(x);
b=zeros(n,n);
b(:,1)=y(:);
%finite difference matrix
for j=2:n
for i=1:n-j+1
b(i,j)= (b(i+1,j-1)-b(i,j-1))/(x(i+j-1)-x(i));
end
end
%interpolation polynom
yint=b(1,1);
xt=1;
for j=1:n-1
xt=xt*(X-x(j));
yint=yint+b(1,j+1)*xt;
end
Y=yint
Sorry, it was supposed to come out like this.
Geoff Hayes
Geoff Hayes 2016 年 8 月 14 日
Vitor - to properly format your code, highlight that text which is code and press the {}Code button.

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

回答 (1 件)

Walter Roberson
Walter Roberson 2016 年 8 月 14 日
Functions cannot return functions. Functions can return function handles, and functions can return symbolic expressions, and functions can return symbolic functions. The Y that you compute is already a symbolic expression; if you want to turn it into a function handle, use matlabFunction

カテゴリ

Help Center および File ExchangeSymbolic Math Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by