登录论坛

查看完整版本 : Pattern from 1997: using feval - Loren on the Art of MATLAB


poster
2022-01-06, 22:44
https://blogs.mathworks.com/wp-content/themes/mathworks_1.0/images/placeholder_5.jpg
In the early 1990s, to avoid eval and all of its quirks (if you don't know about this, DON'T look it up - it's totally discouraged), we recommended using feval for evaluating functions that might not be known until supplied by the user running the code. We used this, for example, for evaluating numeric integrals. We wanted to leave the integrand completely flexible and up to the user. Yet the integrator had to be able to evaluate the user function, an unknown at the time of creating the integrator.function I=integ(fcn,fmin,fmax,tol)if ~ischar(fcn) error(...)end% figure out some initial points to evaluatepts = linspace(fmin, fmax, 20);fv = feval(fcn,pts);I = ...:endThis had the advantage of not asking MATLAB to "poof" any variables into the workspace. It helped also avoid situations where there was a possibility of a function and variable having the same name, thereby possibly not giving you the version of

...read more >> (https://blogs.mathworks.com/loren/?p=5002)



More... (https://blogs.mathworks.com/loren/2022/01/06/pattern-from-1997-using-feval-2/?s_tid=feedtopost)