Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that can calculate a definite integral of a function. If you hav

ID: 2261605 • Letter: W

Question

Write a program that can calculate a definite integral of a function. If you haven't taken Calculus 2, a definite integral is defined as follows: f(x) = F(b)-F(a) where F is the antiderivative of f -i.e. f - d. If the function f is simple, it's possible to find the antiderivative. For example, if f = 2x then F-x2. However, if the function is more complicated, it's not possible to easily find the antiderivative - this is called a nonelementary antiderivative. For example, if f=sin(t), the antiderivative is not obvious. In this case, we can use the following approximation: f (x) Where N =- Your program should take 4 arguments: function a b deltax where function is 1, 2, or 3. Iffunction is 1, f(x) = sin (x). Iffunction is 2, f (x)- If function is 3, f(x)- sin (x2) So, to calculate J. sin (x) with x = 0.0001, your program should do the following: $ ./integral 1 0 3.1415926 0.0001 integral value = 2.000000 To calculate sx2, your program should do the following: $ ./integral 20 20.0001 integral value- 2.666667

Explanation / Answer

%%% Matlab function

function integ(f,a,b,deltax)
I=0;
syms x
if f==1
fx=sin(x);
else if f==2
fx=x^2;
else if f==3
fx=sin(x^2);
end
end
end
N=(b-a)/deltax;
for n=1:N
n1=a+n*deltax;
n2=a+(n+1)*deltax;
I=I+deltax*(subs(fx,n1)+subs(fx,n2))/2;
end
fprintf('Integral value = %f ',I);
end

%%%Function test

>> integ(1,0,pi,0.001)
Integral value = 1.999999
>> integ(2,0,2,0.001)
Integral value = 2.670669

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote