フィルターのクリア

The code runs when if I do; newton_recur(@(x) x^2,@(x) 2*x,1,10) but wont work if I leave out the @(x). is there a way to do this so that the user doesn't need to add @(x)?

1 回表示 (過去 30 日間)
function x = newton_recur(f,df,x0,n)
Xhist = 1:n;
x = x0;
for i = 1:n
x = x - f(x)/df(x);
Xhist(1,i) = x;
end
disp(Xhist)
fprintf('x(%d) = %d', n, x)
end

採用された回答

Walter Roberson
Walter Roberson 2023 年 11 月 9 日
In MATLAB, parameters to a function are always evaluated before being passsed to the function. There is no way in MATLAB to be able to define a function with a parameter marked as "hold for later evaluation".
Therefore if you want the user to be able to call
newton_recur(x^2,2*x,1,10)
then x^2 and 2*x must evaluate to something because they will definitely be evaluated before they are passed in to newton_recur
Not only must the x^2 and 2*x evaluate to something but they must evaluate to something useful -- something that directly be evaluated with different values of x, or else something that can be converted to something that can be evaluated with different values of x.
If, before that point, the user had defined x as a numeric value, then x^2 would be those particular numeric values squared, and you would not be able to (reliably) work back from that to a formula to be evaluated for different values.
If, before that point, the user had defined x as a character scalar such as
x = 'x';
newton_recur(x^2,2*x,1,10)
then x^2 would be a request to square the internal numeric code used to represent the character vector 'x' -- which happens to be 120. The result would be the same as if the user had coded
x = 120;
newton_recur(x^2,2*x,1,10)
which is not going to do anything useful for you.
What can potentially be used?
  • x could be a sym or symfun or symmatrix
  • x could be an optimization variable (see optimvar)
  • I might have overlooked something
Remember though that for all of these possibilities, x would have had to have been defined as being of the appropriate type before the
newton_recur(x^2,2*x,1,10)
call. If x has not been assigned any value before that call, it is not possible for newton_recur to receive control at all in that statement.
So if you do not have the Symbolic Toolbox or the Optimization Toolbox, what are your choices?
  1. Use an @(x) introducer to define an anonymous function; Or
  2. Have the user pass in a character vector or string() scalar, such as newton_recur('x^2', "2*x", 1, 10)
I repeat: in MATLAB it is impossible to define a function that can say "do not evaluate the parameters, just let me see what the user typed there".
(This is not the case for some other symbolic programming languages, such as Maple . In Maple it is possible to define a parameter as being of type uneval to receive what the user typed in for the parameter without having evaluated it as an expression first.)

その他の回答 (1 件)

Steven Lord
Steven Lord 2023 年 11 月 9 日
You mean you want the user to enter 'x^2' and '2*x' and automatically generate the anonymous functions? Take a look at the vectorize, symvar, and str2func functions.
Note that if you want the user to be able to call your function as:
newton_recur(x^2,2*x,1,10)
and have MATLAB "automatically know" that it should convert those expressions into function handles, the only way that would work is if x were previously defined as symbolic variables using the sym function. In that case use matlabFunction to turn the symbolic expression into a function handle.
  1 件のコメント
James Tursa
James Tursa 2023 年 11 月 9 日
Could also have the user enter the expressions directly but have the code read it as text, e.g., via input(prompt,'s'), and then turn that into function handles.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeConversion Between Symbolic and Numeric についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by