0

I had a working bisection method but I need to implement it in a function that will take three arguments: f the function being used, xL the left side of the boundary, and xR the right side of the boundary. Furthermore it has to have the ability to change the function based on the value of x (if the function was f(x) for example). if x is less than or equal zero, f(x) will be x^3 + 3x + 1, if x is greater that zero then f(x) will be 1 + sin(x). The function needs to output the root and the number of iterations performed

How can I pass a function for f is the function arguments, and then how can I find the value of x to determine which function to use?

Here are the errors I am getting along with the console input:

>> f = inline('x^3 + 3x + 1', 'x')

f =

 Inline function:
 f(x) = x^3 + 3x + 1

>> bisectionF(f,-2,0.1)
Undefined function or variable 'bisectionF'.

>> bisectionF(f,-2,0.1)
Undefined function or variable 'B'.

Error in bisectionF (line 3)
maxIters = 20;B

>> bisectionF(f,-2,0.1)
Error using inlineeval (line 14)
Error in inline expression ==> x^3 + 3x + 1
Error: Unexpected MATLAB expression.

Error in inline/feval (line 33)
    INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr,     INLINE_OBJ_.expr);

Error in bisectionF (line 7)
fM = feval(f,xM);

>> y = bisectionF('f1',-2,0.1)
Error using feval
Undefined function 'f1' for input arguments of type 'double'.

Error in bisectionF (line 9)
fM = feval(f,xM);

>> f1 = inline('x^3 + 3x + 1', 'x');
>> root = bisectionF(f1,-2,0.1)
Error using inlineeval (line 14)
Error in inline expression ==> x^3 + 3x + 1
Error: Unexpected MATLAB expression.

Error in inline/feval (line 33)
    INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr,   INLINE_OBJ_.expr);

Error in bisectionF (line 9)
fM = feval(f,xM);

Here is my code:

function[r, iters] = bisectionF(f,xL,xR)
%f1 = inline('x^3 + 3x + 1', 'x');
%f2 = inline('1+sin(x)','x');

maxIters = 20;
precision = 10^(-5);
for j = 1:maxIters
    xM=(xL + xR)/ 2; 
    fM = feval(f,xM);
    fL = feval(f,xL);
    fR = feval(f,xR);
    if fM * fL < 0
       xR = xM
    elseif fM * fR < 0
       xL = xM;
    end
    if abs(fM) < precision
       disp(['Iterations performed: ', num2str(j)])
       disp('Convergence was reached')
    elseif(j==20)
       disp('The maximum number of iterations were performed')
    end
    r = xM;
    iters = j;
end
AlessioX
  • 3,167
  • 6
  • 24
  • 40
cjstittles
  • 61
  • 5
  • Multiplication must be explicit (`*`) – sco1 Feb 11 '16 at 16:09
  • Awesome! I absolutely forgot about that for the syntax. Do you know of a way to have a different function input for x based on what the value of x is? Thank you for your help. – cjstittles Feb 11 '16 at 16:14

1 Answers1

0

You can pass as input not just an inline function, but a set (cell) of inlines. In your main script (or in the Command Window) you can declare such cell array as follows:

f1 = inline('x^3 + 3*x + 1', 'x');
f2 = inline('1+sin(x)','x');
CandidateFunctions{1}=f1;
CandidateFunctions{2}=f2;

and give CandidateFunctions as input to bisectionF (instead of a single function f).

Inside bisectionF now, after the line

xM=(xL + xR)/ 2; 

you can set an if/else switch:

if xM<=0
    f=CandidateFunctions{1}
else
    f=CandidateFunctions{2}
end

In this manner you reassign/overwrite f at each iteration, depending on whether xM is positive, negative or null.

AlessioX
  • 3,167
  • 6
  • 24
  • 40
  • Thank you for your reply! Then what can I feed in as f in the function call? – cjstittles Feb 11 '16 at 17:25
  • You give the entire cell (candidates) as input. Because the `if/else` needs both of them. So you'll have `bisectionF(CandidateFunctions,-2,0.1)` as a call – AlessioX Feb 11 '16 at 17:27
  • Thank you for that. And thank you for answering my rather basic questions. When I enter bisectionF(CandidateFunctions, -2, 0.1) into the command prompt it says CandidateFunctions is undefined. But it is in the function file. How do i fix this? Also if you could how is the command console linked with the function file, meaning what can't it see as already define in a function file and what you have to declare in the command console itself. Thank you! – cjstittles Feb 11 '16 at 17:31
  • Ok sorry, my bad. Since you're working with the Command Window, with "main script" I mean "Command Window". To avoid confusion, clear all the workspace. Then type the 4 commands in the Command Window `f1 = inline('x^3 + 3*x + 1', 'x'); f2 = inline('1+sin(x)','x'); CandidateFunctions{1}=f1; CandidateFunctions{2}=f2;`. These lines must *not* be in the `bisectionF` file. After these 4 lines `CandidateFunction` will be defined and ready to rock in your workspace, so now call `bisectionF` as `bisectionF(CandidateFunctions,-2,0.1)` – AlessioX Feb 11 '16 at 17:35
  • Thank you! What happens if they are defined in the bisectionF file as well? – cjstittles Feb 11 '16 at 17:38
  • If you define `CandidateFunctions` inside `bisectionF` it is absolutely silly to declare it as input parameter. Whatever `CandidateFunctions` you feed to the function, it will always be overwritten by the `CandidateFunctions` you manually declared inside the script itself – AlessioX Feb 11 '16 at 17:41