Skip to main content
Engineering LibreTexts

6.7: String Inputs and the eval Function

  • Page ID
    55661
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    We note that on occasion we do want the actual function to change - the instructions to be evaluated to change - as we change the inputs. This can be done with the eval function. The function eval takes as input a string and returns the evaluation of this string given current values for the various variables present in the string; in essence, eval is in the interpreter.

    For example, we can create the function

    function [ value ] = f_o_diff_eval ( fstring, x, delta_x ) 
    
    z = x; 
    f_x = eval(fstring); 
    z = x + delta_x; 
    f_x_plus = eval(fstring); 
    
    value = (f_x_plus - f_x)./delta_x; 
    
    end
    

    which is our finite difference function but now with a string input fstring to specify the function to be differentiated. Note that eval (fstring) will simply evaluate the expression fstring given the current values of the workspace_f_o_diff_eval.

    We now call \(f_{-}\)o_diff_eval:

    >> fstring = 'z.^4'; 
    >> f_o_diff_eval(fstring,[1,2],.01) 
    ans = 
        4.0604 32.2408 
    >>
    

    which gives us the same result as previously. Note that \(f_{\text {_ }}=\) eval (fstring) in \(f_{-}\)diff_eval for fstring as given is equivalent to \(f_{-} x=z\). 4 but since in the previous line we set \(z=x\) then \(f_{-} x\) is assigned \(x . \sim 4\) as desired. Similarly, two lines down, \(f_{-} x_{-} p l u s\) is assigned the appropriate value since we have changed \(z\) to be \(z=x+\) delta \(x\). The user can now specify any desired function (expressed in terms of \(z\) ) without creating a MATLAB function (or anonymous function).

    In actual practice there are certainly better ways to accomplish these goals. The purpose of this little section is only to illustrate that on occasions in which we would like to adapt the actual code there are some simple ways to implement this feature.


    This page titled 6.7: String Inputs and the eval Function is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Masayuki Yano, James Douglass Penn, George Konidaris, & Anthony T Patera (MIT OpenCourseWare) via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.