In: Computer Science
Please Use matlab and show command window. Please go through all steps
Using a Dialog button with 3 buttons have the user chose one of the 3 anonymous functions below to be used in the later selected three Programmed functions (1) Integral by Trapezoidal Method,(2) Integral by Simpson Method, or (3) Root by BiSection Method.:
a. f(x) = 2*x^5 -3*x^2 – 5
b. f(x) = x^2 – 5
c. f(x) = x^(1/2)
2. Using an input statement ask the user to pick Midpoint, Simpson, or ROOT. Make sure you are consistent with string or number. You can use what you are comfortable with. The input variable will then be used in a switch case statement. Each case will do the following
a. First case (Midpoint Integral) i. Ask for the three inputs a, b, and n ii. Call the function iii. Use fprintf to output the answer
b. Second case (Simpson Integral) i. Ask for the three inputs a, b, and n ii. Call the function iii. Use fprintf to output the answer
c. Third case (ROOTs) i. Ask for the three inputs a, b, and epsilon the convergence limit ii. Call the function iii. Use fprintf to output the answer
function result = Midpoint(f, a, b, samples)
x = linspace(a,b,samples);
dx = (a-b)/samples;
result = 0;
for i = 1:samples
result = result + f(x(i) + dx)*dx;
end
end
function result = Simpson(f,a,b, samples)
dx = (b-a)/samples;
x= linspace(a,b,samples);
result = 0;
for i = 1:samples
if i == 1
result = result +
f(x(i));
else
if mod(i, 2) == 0
result = result + 4 * f(x(i));
else
result = result + 2 *
f(x(i));
end
end
end
result = dx / 3 * result;
end
function result = BiSection(f, a, b, epsilon)
t = (a+b)/2;
while (f(t) > epsilon)
if f(t)*f(b) <
0
a = t;
else
if f(t) * f(a) < 0
b = t;
end
end
t= (a + b)/2;
end
result = t;
end
clc
clear
f1 = @(x) 2*x^5 - 3*x^2 - 5;
f2 = @(x) x^2 - 5;
f3 = @(x) x^(1/2);
disp(spaces(10) + "Your choices: ")
disp("Midpoint" + spaces(5) + "Simpson" + spaces(5) +
"Bisection")
choice = input();
if (choice == "Midpoint")
a = input("Input value of a: ", "s");
a = str2double(a);
b = input("Input value of b: ", "s");
b = str2double(b);
c = input("Input value of n(number of samples):
", "s");
c= str2double(c);
result = Midpoint(f1, a, b, n);
result1 = Midpoint(f2, a, b, n);
result3 = Midpoint(f3, a, b, n);
disp("Value of integral of 2*x^5 - 3*x^2 - 5
within "+a+" and "+b+" is "+ result)
disp("Value of integral of x^2 - 5 within "+a+"
and "+b+" is "+ result1)
disp("Value of integral of x^(1/2) within "+a+"
and "+b+" is "+ result2)
else
if choice == "Simpson"
a = input("Input value of a: ", "s");
a = str2double(a);
b = input("Input value of b: ", "s");
b = str2double(b);
c = input("Input value of n(number of samples):
", "s");
c= str2double(c);
result = Simpson(f1, a, b, n);
result1 = Simpson(f2, a, b, n);
result3 = Simpson(f3, a, b, n);
disp("Value of integral of 2*x^5 - 3*x^2 - 5
within "+a+" and "+b+" is "+ result)
disp("Value of integral of x^2 - 5 within "+a+"
and "+b+" is "+ result1)
disp("Value of integral of x^(1/2) within "+a+"
and "+b+" is "+ result2)
else
a = input("Input value of a: ", "s");
a = str2double(a);
b = input("Input value of b: ", "s");
b = str2double(b);
epsilon = input("Input value of epsilon: ",
"s");
epsilon= str2double(epsilon);
result = BiSection(f1, a, b, epsilon);
result1 = BiSection(f2, a, b, epsilon);
result3 = BiSection(f3, a, b, epsilon);
disp("Value of integral of 2*x^5 - 3*x^2 - 5
within "+a+" and "+b+" is "+ result)
disp("Value of integral of x^2 - 5 within "+a+"
and "+b+" is "+ result1)
disp("Value of integral of x^(1/2) within "+a+"
and "+b+" is "+ result2
end
end