Assign an input to an anonymous function
    21 views (last 30 days)
  
       Show older comments
    
Basically what I'm trying to do is ask the user to input a function of x 'f(x)' then assign it as an anonymous function then have the user input what x value they would like the function to be evaluated at...
This code isn't complete, but heres what I have so far with the output it gives me:
    function [] = NewtsMeth
    syms x
    l=input('Please enter f(x) = ');
    func=@(x) (l);
    Xest=input('Pleae enter an initial guess = ');
    k=func(Xest);
    d=diff(func,x)
    s=k
    end
output:
    >> NewtsMeth
    Please enter f(x) = 6*x-8
    Pleae enter an initial guess = 3
    d =
    6
    s =
    6*x - 8
Obviously this isn't Newton's method but I'm just trying to figure this input/evaluate part out.
0 Comments
Answers (4)
  Steven Lord
    
      
 on 15 Jun 2023
        If you have a string or a char vector containing an equation:
s = '2*x + x^2 + y'
let's vectorize it so it can be called with arrays of data rather than just scalars.
vs = vectorize(s)
We can use the symvar function (which despite the name is in MATLAB itself, not Symbolic Math Toolbox) to identify the variables used in the equation.
vars = symvar(vs)
String operations will let us build the input arguments section of our anonymous function.
inputArguments = "@(" + join(vars, ",") + ") "
Combining the input arguments section and the vectorized equation text by appending strings lets us build the input to str2func.
f = str2func(inputArguments + vs)
We can check that the function returns the correct result by evaluating the anonymous function and performing the same computations manually.
check1 = f(1:10, 2)
check2 = 2*(1:10) + (1:10).^2 + 2
isequal(check1, check2)
0 Comments
  Joy Tian
 on 16 Dec 2014
        But it doesn't explain to me what is @(x) mean. And how does it relate with anonymous function.
1 Comment
  M_A_C
 on 15 Jun 2023
        
      Edited: Walter Roberson
      
      
 on 27 Mar 2025
  
      Hi,
I'm in an annoying loop of errors and discontinued functions.
The original scipt can be found at https://docs.google.com/document/d/1vyy_w8r2hxR-CEMJrooRg4nuO6M_jS8FY9qQRRDCwF0/edit 
The script asks for an input that is a function. That function goes to the argument of the inline function, but it is discontinued. Then I tried to use the Anonymous Function  function, but I keep getting errors because for some reason @() does not allow me to evaluate the equation related to that function. Then I tried to apply the str2func to my input, but str2func is also being discontinued, and I get errors anyways.
This is an excerpt of the code that generates the error: 
clc
e1=input("Enter F(Y)=","s");
% f_c = str2sym(e1);
F=@(Y)  e1;
n=input('Enter No. of Iteration=');
a=input('Enter Initial Guess a=');
b=input('Enter Initial Guess b=');
while (F(a)*F(b)>0)
 I get the error here because F(a) = '2*x + x^2'
1-The original code works pretty well with inline.
2-Please, Can anyone answer how to use input and @()???
4 Comments
  Steven Lord
    
      
 on 27 Mar 2025
				That's okay, I just wanted to make sure you weren't being misled by something phrased incorrectly or ambiguously in the documentation.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!