Question

In: Advanced Math

Write a program to compute 2 point Gauss quadrature on a single interval. Your input should...

Write a program to compute 2 point Gauss quadrature on a single interval. Your input should be a function with f,a,and b. Verify it by checking that the error in using the rule for f(x)=x^3 - 3x^2 +x - 7 is zero. In matlab please.

Solutions

Expert Solution


%Matlab Code for Gaussian Quadrature integration

clear all
close all

%function for Gaussian Quadrature
f=@(x) x.^3 - 3*x.^2 +x - 7;
%for 2 point Gaussian quadrature N=2
N=2;
%upper and lower limit
a=0; b=1;

%integration using gaussian quadrature 2 point
value=gauss_quad(f,N,a,b);

%exact integration
ext_val=integral(f,a,b);

fprintf('For the function f(x)=')
disp(f)

fprintf('Integration value for a=%f to b=%f using Gaussian Quadrature is %f\n',a,b,value)
fprintf('Integration value for a=%f to b=%f using exact integration is %f\n',a,b,ext_val)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%Function for Gaussian quadrature
function value=gauss_quad(f,N,a,b)

%
% This script is for computing definite integrals using Legendre-Gauss
% Quadrature. Computes the Legendre-Gauss nodes and weights on an interval
% [a,b] with truncation order N
%
% Suppose you have a continuous function f(x) which is defined on [a,b]
% which you can evaluate at any x in [a,b]. Simply evaluate it at all of
% the values contained in the x vector to obtain a vector f.

    N=N-1;
    N1=N+1; N2=N+2;

    xu=linspace(-1,1,N1)';

    % Initial guess
    y=cos((2*(0:N)'+1)*pi/(2*N+2))+(0.27/N1)*sin(pi*xu*N/N2);

    % Legendre-Gauss Vandermonde Matrix
    L=zeros(N1,N2);

    % Derivative of LGVM
    Lp=zeros(N1,N2);

    % Compute the zeros of the N+1 Legendre Polynomial
    % using the recursion relation and the Newton-Raphson method

    y0=2;

    % Iterate until new points are uniformly within epsilon of old points
    while max(abs(y-y0))>eps


        L(:,1)=1;
        Lp(:,1)=0;

        L(:,2)=y;
        Lp(:,2)=1;

        for k=2:N1
            L(:,k+1)=( (2*k-1)*y.*L(:,k)-(k-1)*L(:,k-1) )/k;
        end

        Lp=(N2)*( L(:,N1)-y.*L(:,N2) )./(1-y.^2);

        y0=y;
        y=y0-L(:,N2)./Lp;

    end

    % Linear map from[-1,1] to [a,b]
    xx=(a*(1-y)+b*(1+y))/2;    

    % Compute the weights
    w=(b-a)./((1-y.^2).*Lp.^2)*(N2/N1)^2;
    value = sum(w.*f(xx));
end

%---------------End of Code---------------%


Related Solutions

Write a program that encrypts and decrypts the user input. Note – Your input should be...
Write a program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x) given by...
​​​​​1) Write a program that defines a structure that represents a point. Your point structure should...
​​​​​1) Write a program that defines a structure that represents a point. Your point structure should contain members for the X and Y value. Write a function called distance that takes two points as parameters and returns the distance between them. Your program should define & print two points and display the distance between them. Copy your structure without formatting: Copy your distance function without formatting: 2) Write a C program that contains an array that contains the following value:...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input a description of several process schedules (i.e., lists of send, receive or print operations). The output of your program will be a linearization of these events in the order actually performed, annotated with Lamport clock values. The input of the program will be a collection of processes, each with a list of operations to perform. The processes are named p1...pn for some n (you...
Write an assembly language program to input a string from the user. Your program should do...
Write an assembly language program to input a string from the user. Your program should do these two things: ***** I AM POSTING THIS QUESTION FOR THE THIRD TIME NOW,I WANT IT DONE USING INCLUDE IRVINE32.INC***** 1. count and display the number of words in the user input string. 2. Flip the case of each character from upper to lower or lower to upper. For example if the user types in:   "Hello thEre. How aRe yOu?" Your output should be:...
write a program for the decryption of 2-rail fence cipher. For example, the input "Cmhmtmrooeoeoorw" should...
write a program for the decryption of 2-rail fence cipher. For example, the input "Cmhmtmrooeoeoorw" should return "Comehometomorrow", and input "topaesw lyr" should return "two players". in python
Work out and derive Gauss Law Of Electricity, in interval form write what your doing for...
Work out and derive Gauss Law Of Electricity, in interval form write what your doing for each step and write neatly please. Thumbs up for correct and clear answers!!
2) Write an algorithm to compute the area of circles. Your algorithm should prompt the user...
2) Write an algorithm to compute the area of circles. Your algorithm should prompt the user to take the number of circles and their radius values. Then it should compute the areas of each circle. Finally, your algorithm will print both radius and area of all circles into the output. [N.B. you need to use iterative statement for solving the problem. Consider Pi = 3.14159] Input: Area of how many Circles you want to compute? 3 Key in radius values:...
Write a program that asks the user to input a set of floating-point values. When the...
Write a program that asks the user to input a set of floating-point values. When the user enters a value that is not a number, give the user a second chance to enter the value. After two chances, quit reading input. Add all correctly specified values and print the sum when the user is done entering data. Use exception handling to detect improper inputs.5 pts Your code with comments A screenshot of the execution Test Case:       Enter float: 1.0...
Write a program with two input values, Hours and Rate. The program should first calculate Gross...
Write a program with two input values, Hours and Rate. The program should first calculate Gross pay, which is your pay before deductions. The program should then calculate the following three deduction amounts: Federal Tax (20% of Gross), State Tax (5% of Gross), Social Security Tax (6.2% of Gross). Then your program should calculate Net pay, which is your Gross pay minus the three deductions. The output should be all five of the calculated values.
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input...
PYTHON Write a python program that encrypts and decrypts the user input. Note – Your input should be only lowercase characters with no spaces. Your program should have a secret distance given by the user that will be used for encryption/decryption. Each character of the user’s input should be offset by the distance value given by the user For Encryption Process: Take the string and reverse the string. Encrypt the reverse string with each character replaced with distance value (x)...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT