Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %% Problem: pass variables efficiently to f2 without globals or workspace
- % % uncomment and run this once: then comment it and run the code.
- % % I saved the function under the name functiontester.m
- % Aa1 = 1;
- % Bb1 = 2;
- % x0_001 = 0.1
- % x0_002 = 0.2
- % constant_1 = 0.3
- % constant_2 = 0.4
- % save ('w.mat')
- function f1_v3 % parent function that does the solving
- clc
- clear all
- tic
- load w.mat;
- A=rand(100,2)
- % Ive got several loops before this function
- for i=1:100 % with loops before this, I have to call it >10000 times
- x0=[x0_001 * A(i,1), x0_002 * A(i,2)]
- options = optimset('MaxFunEvals', 1000,'MaxIter', 1000, 'Display', 'on');
- [x,fval,exitflag,output,jacobian] = fsolve(@f2v3,x0,options) % calling f2
- end
- toc
- end
- function F=f2v3(x) % almost all my variables are used here
- % pick your poison - either comment load w or globals. I want neither.
- % load w.mat;
- global Aa1 Bb1 constant_1 constant_2
- Aa1 = x(1)
- Bb1 = x(2)
- r_1 = constant_1*Aa1*Bb1
- r_2 = constant_2*Aa1+Bb1
- F = [Aa1 - Bb1
- r_1 - r_2];
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement