Bisection and Fixed Point İterations: Function input

1 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by