Bisection and Fixed Point İterations: Function input

1 回表示 (過去 30 日間)
Abdussamet Ova
Abdussamet Ova 2021 年 2 月 24 日
回答済み: Tejas 2024 年 5 月 24 日
Can you give a hint me to insert a given function. For example I want to ask user to 'give a function', it can be any function. And then I am going to ask 'which method are you going to use?'. According to answer, user will give upper limit, lower limit, and error for the bisection method. And fixed point, max iteration number and error for the iterations method.

回答 (1 件)

Tejas
Tejas 2024 年 5 月 24 日
Hi Abdussamet,
A function or mathematical equation can be accepted as input from the user using an anonymous function. An anonymous function is a function that is not stored in a program file but is associated with a variable whose data type is function_handle.
Here is an example of how to accept input using an anonymous function:
userFunc = input('Please enter a function of x (use @(x) notation, e.g., @(x)x^2-2*x+4): ');
Once the function is stored inside the variable, it can be used just like any mathematical function. Below is an example demonstrating how to use it.
function root = bisectionMethod(userFunc, lowerBound, upperBound, tol)
while (upperBound - lowerBound) / 2 > tol
c = (lowerBound + upperBound) / 2;
if userFunc(c) == 0
break;
elseif userFunc(lowerBound) * userFunc(c) < 0
upperBound = c;
else
lowerBound = c;
end
end
root = (lowerBound + upperBound) / 2;
end
For more information on Anonymous Functions, refer to this documentation: https://www.mathworks.com/help/releases/R2020b/matlab/matlab_prog/anonymous-functions.html .

カテゴリ

Help Center および File ExchangeMATLAB についてさらに検索

製品


リリース

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by