【问题标题】:How to use output variables of a function as input for another function如何使用函数的输出变量作为另一个函数的输入
【发布时间】:2015-11-08 16:57:03
【问题描述】:

我想将函数的输出用作构建多项式的函数的输入:

这是我的代码:

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

单独的第一个功能有效。这意味着如果我从第 13 行开始注释到结束,我的代码就可以工作,并且我得到 c 值,其数量取决于开始时输入的 x 向量的长度。

我想使用该 c 值来构建这种形式的多项式:p(x) = c0 + c^1*x1 + c2^x2 + .... + c(n-1)^x(n-1) 并绘制该多项式,其中点以及 xi,yi 在开头通过 2 个向量作为函数插值的输入给出。

有人可以帮我吗?

【问题讨论】:

    标签: matlab function input output polynomial-math


    【解决方案1】:

    制作一个单独的函数多项式,例如

    function y=polynome(x,c)
      y=sum(c.*x.^(0:length(c)-1));
    

    或者直接使用

    y=sum(c.*x.^(0:length(c)-1)); 
    

    计算系数的多项式 c。

    如果您有多个 x 值,例如

    x=[0:.1:3]';
    
    y=repmat(x,1,numel(c)).^repmat((0:numel(c)-1),numel(x),1)*c';
    

    应该给你多项式的值

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-07
      • 2014-11-20
      • 2011-04-11
      • 2019-05-20
      • 1970-01-01
      • 1970-01-01
      • 2017-04-17
      • 1970-01-01
      相关资源
      最近更新 更多