Assign an input to an anonymous function
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(4 个)
Steven Lord
2023-6-15
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 个评论
Joy Tian
2014-12-16
But it doesn't explain to me what is @(x) mean. And how does it relate with anonymous function.
M_A_C
2023-6-15
编辑:Walter Roberson
2025-3-27
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 个评论
Steven Lord
2025-3-27
That's okay, I just wanted to make sure you weren't being misled by something phrased incorrectly or ambiguously in the documentation.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!