0

I have something like this. Where RC.p14 is some function dependent on some parameters.

        t=ogj.a:ogj.s:ogj.b;
        lk1 = zeros(1,length(t));

        f1=RC.lkPrepare(@RC.p14,l1);

        for i=1:length(t)
            lk1(i)=RC.lk(@RC.p14,l1,t(i),f1);
        end            
    end

    function res=lk(p,l,t,f)           
        res=subs(f, 'z', t)/p(l,t);
    end

   function res=lkPrepare(p,l)
        syms z
        res=diff(1-p(l,z));            
    end

But subs in loop took too much time. And is there a way to prepare function for the loop?

4

1 回答 1

0

Function subs allow substitute several values (e.g. vector or matrix) at single call. In your case, you can do substitution without loop:

    t=ogj.a:ogj.s:ogj.b;

    f1=RC.lkPrepare(@RC.p14,l1);

    lk1=RC.lk(@RC.p14,l1,t,f1);
end

function res=lk(p,l,t,f)
    syms z   
    res=subs(f, z, t)./subs(p(l,z), z, t);
end

Expression subs(p(l,z), z, t) in function lk is used to avoid problems in case if function RC.p14 don't work with multiple inputs.

This solution will work also if output from RC.p14 is vector of size n x 1. In this case, function f1 is derivative of RC.p14 with same size (n x 1). Output from both subs operation in function lk will have size n x m, where m is size of array t.

于 2013-04-28T14:20:35.913 回答