Write a user-defined function with function call m = midpoint_ seq(a,b,tol) wher
ID: 3167486 • 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 (USING 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 programm %%%
clc;
clear all;
close all;
a=4;
b=10;
tol=10^(-3);
m=midpoint_seq( a,b,tol);
m'
OUTPUT:
ans =
3.0000
-0.5000
-2.2500
-3.1250
-3.5625
-3.7813
-3.8906
-3.9453
-3.9727
-3.9863
-3.9932
-3.9966
-3.9983
-3.9991
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.