please show all matlab code involved: Use matlab to plot the functions e^(x/5) a
ID: 3674846 • Letter: P
Question
please show all matlab code involved:
Use matlab to plot the functions e^(x/5) and sin(x). Make approximations to the points where they are equal.
[Midpoint Method] Write a short matlab program which will implement the midpoint method, given initial intervals [ak,bk]. Use the results from the above step to implement the program, and nd the approximations to the points where f(x) = e^(x/5) sin(x) = 0. Let the midpoint method run until f(x) < 107. Count the number of steps that the midpoint method takes for each point.
Explanation / Answer
matlabfun.m
clc
clear all
close all
%% Part a
f1 = @(x) exp(-x/5);
f2 = @(x) sin(x);
fplot(f1,[0 1])
hold on
fplot(f2,[0 1])
hold off;
%% Part b
f = @(x) exp(-x/5) - sin(x);
it = 0;
a = 0;
b = 1;
cond = 1;
while (cond > 0)
it = it + 1;
c = (a+b)/2 ;
if (f(c) < 10^-7)
cond = 0;
else
if (f(a) > 0) && (f(b) < 0)
if f(c) > 0
a = c;
else
b = c;
end
elseif (f(a) < 0) && (f(b) > 0)
if f(c) > 0
b = c;
else
a = c;
end
end
end
end
it
Output :
it =
5
at approximate x = 0.958 and y = 0.818 both function are equal
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.