4-point Gauss Quadrature with MATLAB

In summary, the speaker is seeking help with a programming issue in MATLAB for a numerical analysis course. They have four .m files to calculate the 4-point GQ, but are having trouble modifying the existing routine to call any function.m file. They have tried using handles and single quotes, but are still getting an error. They eventually figure out the solution on their own.
  • #1
stvoffutt
15
0

Homework Statement


I need to evaluate a function using 4-point GQ.


Homework Equations


GQ theory


The Attempt at a Solution


I have got more of a programming issue in MATLAB. I am new to MATLAB. Actually this is for a numerical analysis course where we kind of learn MATLAB on the fly. First, I have 4 .m files to calculate the 4-point GQ which are listed below. Now, I have the program running for a specific function.m file and it calculates correctly. But I want to modify my existing GQ routine such that I can call any function.m file I want to. I think this is more of a syntax issue than anything. Since I have no experience with MATLAB any input or suggestions will be greatly appreciated.
.m files:

Main Program
function gauss_quadFULL(a,b,'integrand')
format longe;

%fhandle = @integrand;
reference_weights = [0.3478548 0.6521452 0.6521452 0.3478548];
reference_nodes = [-0.861136312 -0.339981044 0.3399810436 0.8611363116];
[New_weights, New_nodes] = gauss_quad(a,b,reference_weights,reference_nodes);
FourPoint_Gauss = guass_quadSUM('integrand',New_weights,New_nodes)

---------------------------------------------------------------------------------------------
%This function takes the reference weights/nodes and transforms them to the [-1,1] interval

function [weights, nodes] = gauss_quad(a,b,reference_weights,reference_node)

weights=1/2*(b-a)*reference_weights;
nodes = 1/2*((b-a)*reference_node+(b+a));
[weights, nodes]

---------------------------------------------------------------------------------------------
%Does the sum for GQ using nodes and weights

function result = guass_quadSUM(fhandle,New_weights,New_nodes)

result = 0;
nn = length(New_weights); % We choose weights here instead of nodes because
% in multidemensions nodes is a matrix.
for j=1:nn
result = result + New_weights(j)*feval(fhandle,New_nodes(j));
end

---------------------------------------------------------------------------------------------
%example function
function result = function_f_quad(x)

result = cos(pi/4*x)/sin(pi/4*x)^2;

---------------------------------------------------------------------------------------------

When I call the main function I get the following error:

EDU>> gauss_quadFULL(0,1,function_f_quad)
Error using function_f_quad (line 3)
Not enough input arguments.


Again, if I replace the loop sum by:
result = result + New_weights(j)*function_f_quad(New_nodes(j));
I get the correct answer. My question is how to pass the GQ main routine a new function, say f1.m, that does the same thing but for any function that I have in an .m file? I have tried messing with the handles and single quotes but I keep getting the same error in my program. Please help. Thank you in advance.
 
Physics news on Phys.org
  • #2
I figured it out. Thanks.
 

Related to 4-point Gauss Quadrature with MATLAB

1. What is 4-point Gauss Quadrature?

4-point Gauss Quadrature is a numerical integration method used to approximate the integral of a function. It involves dividing the interval of integration into four smaller subintervals and using a weighted sum of function values at specific points within each subinterval to approximate the integral.

2. How does 4-point Gauss Quadrature work?

4-point Gauss Quadrature works by using a set of predetermined points and weights to calculate the integral of a function. These points and weights are chosen to minimize the error in the approximation and are based on the roots of specific orthogonal polynomials.

3. What is the advantage of using 4-point Gauss Quadrature over other integration methods?

The main advantage of 4-point Gauss Quadrature is its accuracy. It can provide more accurate approximations of integrals compared to other methods, such as the trapezoidal rule or Simpson's rule. It is also more efficient for integrands that have smooth and rapidly varying behavior.

4. How can I implement 4-point Gauss Quadrature in MATLAB?

To implement 4-point Gauss Quadrature in MATLAB, you can use the built-in function "gaussquad" or write your own code using the formula for calculating the weights and points. The code will involve creating a loop to calculate the integral for each subinterval and then summing up the results.

5. Is 4-point Gauss Quadrature suitable for all types of integrals?

No, 4-point Gauss Quadrature is not suitable for all types of integrals. It is most effective for integrals that have smooth and rapidly varying behavior. For integrals with discontinuities or singularities, other methods may be more appropriate.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
905
  • Engineering and Comp Sci Homework Help
Replies
18
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
21
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top