Clear Filters
Clear Filters

Bisection and Fixed Point İterations: Function input

3 views (last 30 days)
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.

Answers (1)

Tejas
Tejas on 24 May 2024
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 .

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!