Write a user-defined function with function call m = midpoint_ seq(a,b,tol) wher
ID: 3167517 • Letter: W
Question
Write a user-defined function with function call m = midpoint_ seq(a,b,tol) where a and b are constants such that a < b, and tol is a specified tolerance. The function first calculates the midpoint m1 of the interval [a, b], then the midpoint m2 of [a, m1], then the midpoint m3 of [a, m2], and so on. The process terminates when two successive midpoints are within tol of each other. Allow a maximum of 20 iterations. The output of the function is the sequence m1, m2, m3, . . . . Execute the function for a = 4, b = 10, tol = 10^3. PLEASE ANSWER IN MATLAB.
Explanation / Answer
%%% Matlab function %%%%
function [m]=midpoint_seq( a,b,tol)
for n=1:20
m(n)=(b+a)/2;
m(n+1)=(m(n)+a)/2;
b=m(n);
if (abs(m(n)-m(n+1))< tol )
break;
end
end
%%% main progrmme
clc;
clear all;
close all;
format long
% % % % % restoredefaultpath %%%%%
a=-4;
b=10;
tol=10^(-3);
m=midpoint_seq( a,b,tol);
m'
OUTPUT:
ans =
3.000000000000000
-0.500000000000000
-2.250000000000000
-3.125000000000000
-3.562500000000000
-3.781250000000000
-3.890625000000000
-3.945312500000000
-3.972656250000000
-3.986328125000000
-3.993164062500000
-3.996582031250000
-3.998291015625000
-3.999145507812500
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.