Creating a function handle for each element in a vector (Matlab) -
i have following issue. trying create function handle, vector. in particular, have this
eq0 = @(w) m1.^2*(exp(w))-m2.^2
where m1 , m2 vectors of same dimension. so, each m1(i) , m2(i) want have handle w(i). need in order find w(i)'s in next step using fsolve in looking this
n=size(m1) x0 = zeros(n); wbar = fsolve(eq0,x0)
i have tried using arrayfun, received following error
eq0 = arrayfun( @(w) m1.^2*(exp(w))-m2.^2, m1=m1e, m2=m2e) error: expression left of equals sign not valid target assignment.
another attempt in using arrayfun resulted in (here used m1 , m2 vectors directly, not inputs in previous case)
eq0 = arrayfun( @(w) m1.^2*(exp(w))-m2.^2,:) undefined variable arrayfun.
i missing something. have looked on feeds on arrayfun looks problem different.
any advice appreciated.
so if understood right want have each m1(i) or m2(i) seperate function handle eq0(i) can operate vector w in following way eq0(i) = @(w) m1(i)^2*(exp(w))-m2(i)^2. correct? if can create cell-array of same dimension m1 function handle in each dimension:
eq0 = cell(size(m1)); ii = 1:numel(m1) eq0(ii) = {@(w) m1(ii)^2*exp(w)-m2(ii)^2}; end
edit: option be:
eq0 = @(w)arrayfun(@(wel,m1el,m2el)m1el^2*exp(wel)-m2el^2,w,m1,m2); fsolve(eq0, w_values)
here m1, m2 should defined beforehand. otherwise have add them arguments of first anonymous function. calling arrayfun(@(wel, m1el, m2el)..., w, m1, m2)
elementwise calculations entries in w, m1, m2
defined anonymous function handle have passed in first argument of arrayfun. since want define w
differently each time, make anonymous function of arrayfun command, takes w
argument.
Comments
Post a Comment