如何调用多个依赖函数?

时间:2017-01-16 00:21:16

标签: matlab octave

我想在代码中调用这样的多个依赖函数。

function  [k11,k12] = k_fun()
% syntax is : function [outputs] = function-name(inputs)
  a=2.0;
  k11 = quad(B1,B1,a); %error
% For k11,  I want to call function b_fun and select only B1,B2 to pass to function quad.
  k12 = quad(B1,B2,a);
 endfunction

 function  [B] = b_fun(x)
  B1 = -0.5+x;
  B2 = 2.0*x;
  B3 = 0.5+x;
  B=[B1,B2,B3];
  endfunction


 function  [value] = quad(B_i,B_j,a)
   value=0
   points = [0.57,-0.57];
   wt=[1.0,1.0]
   for (ct=1:1:2)
    value = value + (B_i(points(ct))*B_j(points(ct))*a)*wt(ct);
   end
 endfunction

我想运行函数k_fun。在a=2.0之后它将转到k11行。对于k11,我希望从B1获取B2b_fun(),并将其传递给quad()。在quad()中,将评估函数。

我想做 k12 = B1(0.57)*B2(0.57)*a*1 + B1(-0.57)*B2(-0.57)*a*1。这些值+ -0.57来自函数quad。我没有了解如何在B1行中传递B2%error。我在调用依赖函数时遇到困难。我想要保持相同的程序格式(在各自的函数中定义变量),因为原始程序是这样的。后来,我想将这个程序翻译成C ++,所以想使用编程语言的标准函数而不是特定的函数。

顺便说一下,我在Octave中启动了程序,所以endfunction而不是MATLAB中的end

2 个答案:

答案 0 :(得分:1)

您最好的选择是重新组织一下,以便为每个.container { display: flex; display: -moz-flex; display: -webkit-flex; display: -ms-flex; flex-wrap: wrap; justify-content: flex-end; align-items: stretch; } section { display: flex; display: -moz-flex; display: -webkit-flex; display: -ms-flex; padding: 15px; min-width: 350px; flex: 1; margin-top: 3vw; margin-left: 6vw; } aside { min-width: 250px; flex: 0.5; margin-top: 5vw; display: flex; display: -moz-flex; display: -webkit-flex; display: -ms-flex; flex-flow: column nowrap; justify-content: flex-start; align-items: stretch; } 变量设置单独版本的.container { display: -webkit-box; display: -moz-box; display: box; display: -webkit-flex; display: -moz-flex; display: -ms-flexbox; display: flex; -webkit-box-lines: multiple; -moz-box-lines: multiple; box-lines: multiple; -webkit-flex-wrap: wrap; -moz-flex-wrap: wrap; -ms-flex-wrap: wrap; flex-wrap: wrap; -webkit-box-pack: end; -moz-box-pack: end; box-pack: end; -webkit-justify-content: flex-end; -moz-justify-content: flex-end; -ms-justify-content: flex-end; -o-justify-content: flex-end; justify-content: flex-end; -ms-flex-pack: end; -webkit-box-align: stretch; -moz-box-align: stretch; box-align: stretch; -webkit-align-items: stretch; -moz-align-items: stretch; -ms-align-items: stretch; -o-align-items: stretch; align-items: stretch; -ms-flex-align: stretch; } section { display: -webkit-box; display: -moz-box; display: box; display: -webkit-flex; display: -moz-flex; display: -ms-flexbox; display: flex; padding: 15px; min-width: 350px; -webkit-flex-grow: 1; -moz-flex-grow: 1; flex-grow: 1; -ms-flex-positive: 1; margin-top: 3vw; margin-left: 6vw; } aside { min-width: 250px; flex: 0.5; margin-top: 5vw; display: -webkit-box; display: -moz-box; display: box; display: -webkit-flex; display: -moz-flex; display: -ms-flexbox; display: flex; -webkit-flex-flow: column nowrap; -moz-flex-flow: column nowrap; flex-flow: column nowrap; -webkit-box-pack: start; -moz-box-pack: start; box-pack: start; -webkit-justify-content: flex-start; -moz-justify-content: flex-start; -ms-justify-content: flex-start; -o-justify-content: flex-start; justify-content: flex-start; -ms-flex-pack: start; -webkit-box-align: stretch; -moz-box-align: stretch; box-align: stretch; -webkit-align-items: stretch; -moz-align-items: stretch; -ms-align-items: stretch; -o-align-items: stretch; align-items: stretch; -ms-flex-align: stretch; } 。然后,您可以为这些b_fun创建一个函数句柄,并将 传递给B*

b_fun

或者,您可以创建quadfunction [k11, k12] = k_fun() a = 2.0; k11 = quad(@b1_fun, @b1_fun, a); k12 = quad(@b1_fun, @b2_fun, a); end function result = b1_fun(x) result = x - 0.5; end function result = b2_fun(x) result = 2 * x; end 匿名函数,因为它们非常简单

B1

答案 1 :(得分:0)

这是另一种方法:

B = @(x) [-0.5 + x ; 
          2.0 .* x ;
          0.5 +  x ];

现在您可以将行向量传递给B并获取一列输出!所以

points = [0.57,-0.57];

B(points) =
    0.0700   -1.0700
    1.1400   -1.1400
    1.0700   -0.0700

然后你说你想要实际计算:

k12 = B1(0.57)*B2(0.57)*a*1 + B1(-0.57)*B2(-0.57)*a*1

在这个新结构下,

k12 = B(1,1)*B(2,1)*a*1 + B(1,2)*B(2,2)*a*1

我不确定你为什么要离开*1,但我保留了它们的完整性。总而言之,您只需拥有一个B_output = B(points)变量,现在可以通过B2访问B_output(2,i),其中i是与points对应的列索引指数。如果您只通过B(x)一点,您可以说B2 = B_output(2)

总结

您的整个代码可以写成:

a = 2.0;
B = @(x) [-0.5 + x ; 
          2.0 .* x ;
          0.5 +  x ];

k11 = quad(B, 1, 1, a);
k12 = quad(B, 1, 2, a);

function value = quad(B, i, j, a)

    value = 0;
    points = [0.57,-0.57];
    wt = [1.0,1.0];

    B_output = B(points);

    for ct = 1:2

        value = value + B_output(i, ct)*B_output(j, ct)*a*wt(ct);

    end

end