如何为同一个变量赋值,但在matlab中为不同的相应函数赋值?

时间:2016-09-26 14:35:23

标签: matlab function variables parameters

假设我有一个要解决的功能,如下面的代码所示。如何为每个参数betan1n2n3设置n4的值(请参阅代码)?

a= 0.1;
i = 1;
m = 0;    
k = 1;
beta = 1;

while m(i) <= 10    
    n1 = a(i);
    n2 = a(i)+1*n1;
    n3 = a(i)+2*n1+3*n2);
    n4 = a(i)+4*n1+5*n2+6*n3);
    a(i+1) = a(i) + 2*n1 + 4*n2 + 12*n3 + 13*n4;
    %something like
    %beta = [1 2 3 4]
    %m(i+1) = m(i) + beta[1]*k for n1
    %m(i+1) = m(i) + beta[2]*k for n2
    %m(i+1) = m(i) + beta[3]*k for n3
    %m(i+1) = m(i) + beta[4]*k for n4
    m(i+1) = m(i) + beta*k; % k is the same for all n1, n2, n3, n4. But I want to assign different beta to n1, n2, n3, n4, say beta = 1,2,3,4 for n1, n2, n3, n4, respectively. 
    i = i +1;
end

&#39; m(i)&#39;用于为[n1 n2 n3 n4]中的每个n分配不同的时间步长。我不确定这是否可以分配不同的值&#39; m&#39;到了&#39; n&#39;每次它发展。

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

这是你尝试做的吗?

a = 0.1;
an = [1 1+1 1+2+3*2 1+4+5*2+6*9].';
ii = 1;
m = zeros(4,1);    
n = m;
beta = [1 2 3 4].';
v = [2 4 12 13].';

while m(ii) <= 10 % <-- this should change to somthing that makes sense.
    n(:,ii) = a(ii)*an; % this vector replaces n1 by n(1), n2 by n(2) etc...
    a(ii+1) = a(ii) + sum(v.*n(:,ii));
    m(:,ii+1) = m(:,ii) + beta.*n(:,ii);
    ii = ii+1;
end

我不确定&#34; n1,n2,n3,n4&#34; 的不同beta是什么意思,因为n上没有操作beta,所以我猜你想要用相关的k切换n