Assign an input to an anonymous function

13 Ansichten (letzte 30 Tage)
Dave Regan
Dave Regan am 1 Dez. 2014
Beantwortet: Steven Lord am 15 Jun. 2023
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.

Akzeptierte Antwort

Dave Regan
Dave Regan am 2 Dez. 2014
Bearbeitet: Dave Regan am 2 Dez. 2014
I did it like this...
function []=test
syms x
digits(9);
func= input('Please enter f(x) = ');
Xest= input('Pleae enter an initial guess = ');
d=diff(func,x);
x=Xest;
for i = 0:15
ds=eval(d);
fs=eval(func);
x = x-((fs)./(ds));
vpa(x)
if x(i-1)==x(i)
break
end
end
end
Didn't end up using anonymous function.
The if loop is a little messed up beacuse I'm trying to find a way to break it once the answer repeats but I basically just changed x from symbolic and assigned the value of the guess to it then evaluated the function using "eval()"
I'm sure there's a better way to do it.

Weitere Antworten (3)

Joy Tian
Joy Tian am 16 Dez. 2014
But it doesn't explain to me what is @(x) mean. And how does it relate with anonymous function.

M_A_C
M_A_C am 15 Jun. 2023
Hi,
I'm in an annoying loop of errors and discontinued functions.
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 the exert 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 @()???
  1 Kommentar
M_A_C
M_A_C am 15 Jun. 2023
I just found a solution, but still using str2func!
Instead of entering 2*x + x^2 I input @(x) 2*x + x^2
and then F = str2func(e1)
Any suggestions to avoid that type of input and str2func are wellcome.
Thank you

Melden Sie sich an, um zu kommentieren.


Steven Lord
Steven Lord am 15 Jun. 2023
If you have a string or a char vector containing an equation:
s = '2*x + x^2 + y'
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)
vs = '2.*x + x.^2 + y'
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)
vars = 2×1 cell array
{'x'} {'y'}
String operations will let us build the input arguments section of our anonymous function.
inputArguments = "@(" + join(vars, ",") + ") "
inputArguments = "@(x,y) "
Combining the input arguments section and the vectorized equation text by appending strings lets us build the input to str2func.
f = str2func(inputArguments + vs)
f = function_handle with value:
@(x,y)2.*x+x.^2+y
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)
check1 = 1×10
5 10 17 26 37 50 65 82 101 122
check2 = 2*(1:10) + (1:10).^2 + 2
check2 = 1×10
5 10 17 26 37 50 65 82 101 122
isequal(check1, check2)
ans = logical
1

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by