matlab - How to load variables from a parent code? -
i have following "parent" code, call solve.m
:
s1 = .01; v = 1; fun1 = @rootd; x0 = [.2, .3, .4] options = optimset('maxfunevals',100000,'maxiter', 10000 ); x1 = fsolve(fun1,x0, options)
i have following code function rootd.m
function d = rootd(x1) f = @(p) 1 - ((3.*x1(3).^2 - 7.*x1(3) + 6).*(x1(2)./p -1))./(2.*x1(3).*(1-x1(3))); h = @(p) (1 - ((3.*x1(3).^2 - 7.*x1(3) + 6).*(x1(2)./p -1))./(2.*x1(3).*(1-x1(3)))).^2; f1 = @(p) 1 - ((6 - 7.*x1(3) + 3.*x1(3)^2).*(min(v,x1(2))./p - 1 ))./(2.*x1(3).*(1-x1(3))); h1 = @(p) (1 - ((6 - 7.*x1(3) + 3.*x1(3)^2).*(min(v,x1(2))./p - 1))./(2.*x1(3).*(1-x1(3)))).^2; g = @(p) integral(@(p) f(p), x1(1), x1(2)); f = @(p) integral(@(p) f1(p), x1(1), (min(v,x1(2)))); h = @(p) integral(@(p) h1(p), x1(1),(min(v,x1(2)))); d(1) = g(x1) - s1; d(2) = x1(3).*(2-x1(3)).*(v - min(v, x1(2))) - (((x1(3)^2)/2).*h(x1)) + (2.*x1(3)... - (1/2).*x1(3).^2).*f(x1) - ((v-min(v, x1(2))) + f(x1) -s1); d(3) = (6 - 7.*x1(3) + 3.*x1(3)^2).*min(v, x1(2))./((2-x1(3)).*(3-x1(3))) - x1(1); end
when run parent code, obtain:
undefined function or variable 's1'.
i understand need define variable s1
in rootd.m
function. function automatically loads s1
solve.m
.
unless make variable global can not access it. might not best idea in matlab.
you can pass variable in parameter function.
s1 = .01; fun1 = @(pars)rootd(s1,pars);
this defines rootd
single parameter function (something several of optimization function require), although can pass multiple one. rootd
function can this:
function d = rootd(s1,x1) ... end
Comments
Post a Comment