简体   繁体   中英

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.

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.

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.

If you have multiple values of x, eg

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM