[英]How to use output variables of a function as input for another function
I would like to use the output of a function as input for a function that builds a polynom: 我想将函数的输出用作构建多项式的函数的输入:
here is my code: 这是我的代码:
function c = interpolation(x, y)
n = length(x);
V = ones(n);
for j = 2:n
V(:,j) = x.*V(:,j-1);
end
c = V \ y;
disp(V)
for i = 0:n-1
fprintf('c%d= %.3f\n', i, c(i+1));
end
polynome(c);
function p = polynome(x)
n = length(x);
for l= 0:n-1
polynome = polynome * x^l;
end
end
The first function alone, works. 仅第一个功能有效。 That means my code works if I comment starting from line 13 to end, and I get the c values, whose number depends from the length of the entered x vector in the beginning.
这意味着如果我从第13行开始注释到我的代码,我的代码将起作用,并且得到c值,其值取决于开头输入的x向量的长度。
I want to use that c values, to build a polynom of this form: p(x) = c0 + c^1*x1 + c2^x2 + .... + c(n-1)^x(n-1)
and plot that polynom, with the points aswell xi,yi given at the beginning through the 2 vectors as input of the function interpolation. 我想使用该c值来构建这种形式的多项式:
p(x) = c0 + c^1*x1 + c2^x2 + .... + c(n-1)^x(n-1)
并绘制该多项式,并在开始时通过2个向量给出xi,yi点作为函数插值的输入。
Can someone help me here? 有人可以帮我吗?
make a separate function polynome eg 制作一个单独的函数多项式,例如
function y=polynome(x,c)
y=sum(c.*x.^(0:length(c)-1));
or just use 或只是使用
y=sum(c.*x.^(0:length(c)-1));
to compute the polynome for your coefficients c. 计算系数的多项式c。
If you have multiple values of x, eg 如果您有多个x值,例如
x=[0:.1:3]';
y=repmat(x,1,numel(c)).^repmat((0:numel(c)-1),numel(x),1)*c';
should give you the values of the polynome 应该给你polynome的价值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.