《matlab优化的参数传递》由会员分享,可在线阅读,更多相关《matlab优化的参数传递(3页珍藏版)》请在金锄头文库上搜索。
1、1. 初学者最爱用但是最不提倡的方法:全局变量法 适用于 MATLAB7 以及以前或者以后的版本 function withaddtionpara1 global b c b = 2; c = 3.5; x0 =0; options = optimset(Display, off); % Turn off Display y = fsolve(poly, x0, options) function y = poly(x) % Compute the polynomial. global b c y = x3 + b*x + c; 2. 直接传递法 适用于 MATLAB7 以及以前或者以后的版本
2、 根据: X=FSOLVE(FUN,X0,OPTIONS,P1,P2,.) passes the problem-dependent parameters P1,P2,. directly to the function FUN: FUN(X,P1,P2,.). Pass an empty matrix for OPTIONS to use the default values. 如果 options 不需要设置,则用空矩阵()占位 function withaddtionpara2 b = 2; c = 3.5; x0 =0; options = optimset(Display, off)
3、; % Turn off Display y = fsolve(poly, x0, options,b,c) function y = poly(x,b,c) % Compute the polynomial. y = x3 + b*x + c; 3. 采用 evalin 以及 assignin 函数法 适用于 MATLAB7 以及以前或者以后的版本 function withaddtionpara3 b = 2; c = 3.5; x0 =0; assignin(base,b,b); assignin(base,c,c); options = optimset(Display, off);
4、% Turn off Display y = fsolve(poly, x0, options) function y = poly(x) % Compute the polynomial. b = evalin(base,b); c = evalin(base,c); y = x3 + b*x + c; 4. 匿名函数法(Anonymous function) 适用于 MATLAB7 以及以后的版本 function withaddtionparas4 options = optimset(Display, off); % Turn off Display b =2; c = 3.5; x0
5、 =0; y = fsolve(x)mypoly(x,b,c), x0, options) function y = mypoly(x,b,c) y = x3 + b*x + c; 5. 嵌套函数法(nested function) 适用于 MATLAB7 以及以后的版本 function withaddtionpara5 b =2; c = 3.5; x0 =0; options = optimset(Display, off); % Turn off Display y = fsolve(poly, x0, options) function y = poly(x) % Compute t
6、he polynomial. y = x3 + b*x + c; end end 6. 采用文件传递(有些太笨拙了的办法) 适用于 MATLAB7 以及以前或者以后的版本 function withaddtionpara6 b = 2; c = 3.5; x0 =0; save paras b c options = optimset(Display, off); % Turn off Display y = fsolve(poly, x0, options) function y = poly(x) % Compute the polynomial. load paras.mat y = x3 + b*x + c;