Solving Integral Approximations with MatLab

  • MATLAB
  • Thread starter renolovexoxo
  • Start date
  • Tags
    Integral
In summary, you can approximate the function and its derivatives using a Taylor series expansion, and then use the error formula to calculate the error.
  • #1
renolovexoxo
25
0
I have this problem to do, involving estimating the error for the derivative as well as the Gaussian Approximation. I am having a lot of trouble figuring out if I can alter this code or if I have to write something new. This is the first time I've ever used MatLab, and I could use some help if anyone has any ideas. The assignment is attached. I used this, which was given in class, to do (ii)

% f(x), the function to integrate
% f= @(x) x^4-2*x ;
% f= @(x) exp(x);
f=@(x) sin(x);
% a, the lower limit of integration
a=0 ;
% b, the upper limit of integration
b=pi ;
% b=1.0;
% n, the number of segments. Note that this number must be even.
% n=20 ;
%************************************************* *********************
format long g
h=(b-a)/n ;
% Sum the odd index function values, and multiply by 4
sumOdd=0 ;
for i=1:2:n-1
sumOdd=sumOdd+f(a+i*h) ;
end
% Sum the even index function values, and multiply by 2
sumEven=0 ;
for i=2:2:n-2
sumEven=sumEven+f(a+i*h) ;
end
sum=4*sumOdd+2*sumEven+f(a)+f(b) ;
% Then multiply by h/3
approx=h/3*sum ;
%exact = quad(f,a,b) ;
%exact=exp(b)-exp(a);
exact=-cos(b)-(-cos(a));
error=abs(approx-exact);
disp(approx);
disp(exact);
disp(error);
 
Physics news on Phys.org
  • #2
</code>The problem I need to doA:So for the derivative, you want to calculate two things:The numerical derivative of your function at some point <code>x0</code>.The error in this derivative.To calculate the numerical derivative, we use the central difference formula:<code>f'(x0) = (f(x0+h) - f(x0-h)) / (2*h)</code>where <code>h</code> is a small number.To calculate the error, you can look at the formula itself. Note that the derivatives of the function around the point <code>x0</code> can be approximated using a Taylor series expansion, i.e.<code>f(x0+h) ~= f(x0) + h*f'(x0) + 1/2*h^2*f''(x0) + ...f(x0-h) ~= f(x0) - h*f'(x0) + 1/2*h^2*f''(x0) + ...</code>Substituting this back into the central difference formula, we get:<code>f'(x0) ~= 1/2*h*(f(x0+h) + f(x0-h)) + 1/8*h^3*f'''(x0) + ...</code>From here, you can use the error formula for numerical differentiation:<code>error ~= 1/12*h^2*f'''(x0)</code>So all you have to do is calculate <code>f'''(x0)</code> and plug it into the error formula.For the Gaussian approximation, you can use the same technique as before. First calculate the numerical integral using the trapezoidal rule, then use a Taylor series expansion around the point <code>x0</code> to approximate the function and its derivatives. Finally, use the error formula for numerical integration:<code>error ~= 1/12*h^2*f''''(x0)</code>where <code>h</code> is the step size
 

1. What is an integral approximation?

An integral approximation is a numerical method used to estimate the value of a definite integral. It involves breaking down the integral into smaller, simpler parts and using mathematical techniques to calculate an approximate value.

2. How does MatLab help with solving integral approximations?

MatLab is a powerful computational software that offers various functions and tools for solving mathematical problems, including integral approximations. It has built-in functions for calculating integrals, as well as tools for visualizing and analyzing the results.

3. What are the benefits of using MatLab for solving integral approximations?

Using MatLab for solving integral approximations offers several benefits. It allows for faster and more accurate calculations compared to manual methods. MatLab also offers a wide range of functions and tools that can handle complex integrals, making it a versatile tool for various applications.

4. Can MatLab handle both one-dimensional and multi-dimensional integrals?

Yes, MatLab has the capability to handle both one-dimensional and multi-dimensional integrals. It has functions like "integral" and "quad" for one-dimensional integrals, and "integral2" and "quad2d" for double integrals. It also has tools for handling triple and higher-order integrals.

5. Are there any tips for improving the accuracy of integral approximations in MatLab?

Yes, there are a few tips for improving the accuracy of integral approximations in MatLab. First, it is important to choose an appropriate integration method for the type of integral being solved. Additionally, using smaller intervals and increasing the number of function evaluations can also improve accuracy. It is also recommended to check the results with analytical solutions, if possible, to ensure accuracy.

Similar threads

  • Calculus and Beyond Homework Help
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
983
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
801
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
Back
Top