Question

Solve the following problem using the MATLAB environment Write a function [approx_root, num_its] = bisection(f,a,b,tol) that...

Solve the following problem using the MATLAB environment

Write a function [approx_root, num_its] = bisection(f,a,b,tol) that implements the bisection method. You function should take as input 4 arguments with the last argument being optional, i.e, if the user does not provide the accuracy tol use a default of 1.0e-6 (use varargin to attain this). Your function should output the approximate root, approx_root and the number of iterations it took to attain the root, num_its. However, if the user calls the function with one argument, your function should return a vector consisting of the approximate root and the number of iterations (see how we used nargout in class). In addition, your function should display and error message and terminate if the user provides an interval without a root. Test your method to solve the following:

(a) x x = 50 on [3, 4]

(b) ln(x) = cos(x) on [1, 2]

In each case first find an interval of size 1 that contains the root, sketch the graphs to determine this. You should also provide tests showing

(a) Your error code handling error messages is correct.

(b) Run the code with the different options for input and output.

(c) Run your code with tol = 1.0e-12 and the default, tol = 1.0e-06.

Homework Answers

Answer #1

This is the required function:

function[approx_root,num_its]= bisection(f,a,b,tol=10^(-6))
if(f(a)*f(b)>0)
printf("\n There is no root in the interval")
return;
endif
dif=f(a)-f(b);
iter=0;

while(abs(dif)>tol)
c=(a+b)/2;

num_its=num_its+1;
if(f(a)*f(c)>0)
a=c;

elseif(f(b)*f(c)>0)
b=c;

elseif(f(c)==0)

break;
elseif(abs(dif2)<10^(-6))

break;
endif

dif=f(a)-f(b)
dif2=a-b
endwhile

approx_root=(a+b)/2

v=[approx_root,num_its]

disp(v)
endfunction

We can call this using:

f=@(x) x.^x.-50

g=@(x) log(x).-cos(x)

bisection(f,3,4)

bisection(g,0,1)

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Solve the following problem in the text book using Excel or Matlab. Calculate the work of...
Solve the following problem in the text book using Excel or Matlab. Calculate the work of mechanically reversible, isothermal compression of 1 mole of methyl chloride from 1 bar to 55 Bar at 100 C. Base calculations on the following forms of the virial equations. The submitted files should include: all input data, calculations and iterations performed in details. Comparison with results obtained from the built in function “If analysis” or “Solver “in Excel. Plot the error versus the number...
10.16: Write a user-defined MATLAB function that solves a first-order ODE by applying the midpoint method...
10.16: Write a user-defined MATLAB function that solves a first-order ODE by applying the midpoint method (use the form of second-order Runge-Kutta method, Eqs(10.65),(10.66)). For function name and arguments use [x,y]=odeMIDPOINT(ODE,a,b,h,yINI). The input argument ODE is a name for the function that calculates dy/dx. It is a dummy name for the function that is imported into odeMIDPOINT. The arguments a and b define the domain of the solution, h is step size; yINI is initial value. The output arguments, x...
Write a MATLAB function and test bench script code to solve the above simple RL/RC circuits...
Write a MATLAB function and test bench script code to solve the above simple RL/RC circuits by following the instructions noted below. The input signal and impulse response generation should be done in the function. The test bench script should be used only to call the function and for signal plotting purposes. No plotting should be done inside the function itself. Name your function L2_C Instructions: Input voltage ,x(t), can be AC or DC. Consider a variable ‘w1’ which can...
Curve-Fit Function USING MATLAB Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads...
Curve-Fit Function USING MATLAB Using the top-down design approach, develop a MATLAB function A8P2RAlastname.m that reads data from a file and performs regression analysis using polyfit and polyval. The function shall have the following features: The input arguments shall include the file name (string), a vector of integers for the degrees of polynomial fits to be determined, and an optional plot type specifier (‘m’ for multiple plots, ‘s’ for a single plot - default). The data files will be text...
You will write a program that loops until the user selects 0 to exit. In the...
You will write a program that loops until the user selects 0 to exit. In the loop the user interactively selects a menu choice to compress or decompress a file. There are three menu options: Option 0: allows the user to exit the program. Option 1: allows the user to compress the specified input file and store the result in an output file. Option 2: allows the user to decompress the specified input file and store the result in an...
I've posted this question like 3 times now and I can't seem to find someone that...
I've posted this question like 3 times now and I can't seem to find someone that is able to answer it. Please can someone help me code this? Thank you!! Programming Project #4 – Programmer Jones and the Temple of Gloom Part 1 The stack data structure plays a pivotal role in the design of computer games. Any algorithm that requires the user to retrace their steps is a perfect candidate for using a stack. In this simple game you...