将值从函数外推到for循环,然后将其传递回函数

时间:2018-06-29 18:19:32

标签: matlab function loops ode

function Test()

a = 2;
b = 1;
c = 0.5;
q = 0.001;
r = 10;

function F = Useful(x) %calculates existing values for x with size 11

eq1 = (1*(0.903*x(2))^(-1))-(0.903*x(1));  
eq2 = (1*(0.665*x(3))*(0.903*x(2))^(-1))-0.903*x(4); 
eq3 = (1*(0.399*x(5))*(0.903*x(2)))-0.665*x(6);
eq4 = (1*(0.399*x(5))*(0.903*x(2))^2)-0.903*x(7);
eq5 = (1*(0.399*x(5))*(0.903*x(2))^3)-1*x(8);
eq6 = (1*(0.665*x(3))*(0.399*x(5))*(0.903*x(2)))-1*x(9);
eq7 = (1*(0.665*x(3))*(0.399*x(5))*(0.903*x(2))^2)-0.903*x(10);
eq8 = (1*(0.665*x(3))*(0.399*x(5)))-0.903*x(11);
eq9 = x(3)+x(4)+x(9)+x(10)+x(11)-a;
eq10 = x(5)+x(6)+x(7)+x(8)+x(9)+x(10)+x(11)-b; 
eq11 = x(2)+x(6)+2*x(7)+3*x(8)+x(9)+2*x(10)-x(1)-x(4)-c;

F = [eq1;eq2;eq3;eq4;eq5;eq6;eq7;eq8; eq9; eq10; eq11];

end

Value(1,1) = 0;

 for d = 2:100 

    x = fsolve(@Useful,x0,options);   %Produces the x(1) to x(11) values

    Value(1,d) = (x(3)+x(5))*d+Value(1,d-1);  %Gives a new value after each iteration

    a = a-x(3);
    b = b-x(5);
    c = c-x(2);

 end

 function Zdot = rhs(t,z) %z = (e1,e2,e3,e4,e5)
 Zdot=zeros(5,1);

 Zdot(1) = -1*z(1);
 Zdot(2) = 1*z(1); 
 Zdot(3) = 1*z(1) - 1*z(2)*z(3);
 Zdot(4) = 1*1*z(1) - Value(1,100)*H(z(3))*z(4)*z(4); 
 Zdot(5) = Value(1,100)*H(z(3))*(z(4));
 end

 function hill = H(x)  
 hill = q/(q+x^r);
 end

 [T,Y] = ode15s(@rhs, [0, 120], [1, 0, 1, 0, 0]); %Solve second function with values giving z(1) to z(5)
 plot(T,Y(:,5))
end 

我想知道,是否有可能将获得的每个值(值(1),值(2)等等)传递给“函数Zdot”,还是只有最终值可以传递?从本质上讲,这可以实现:

 function Zdot = rhs(t,z) %z = (e1,e2,e3,e4,e5)
     Zdot=zeros(5,1);

     Zdot(1) = -1*z(1);
     Zdot(2) = 1*z(1); 
     Zdot(3) = 1*z(1) - 1*z(2)*z(3);
     Zdot(4) = 1*1*z(1) - Value(1,d)*H(z(3))*z(4)*z(4); 
     Zdot(5) = Value(1,d)*H(z(3))*(z(4));
     end

任何见解将不胜感激,我将非常感谢。预先谢谢你!

0 个答案:

没有答案