Results 1 to 2 of 2

Math Help - Please correct the program

  1. #1
    Newbie
    Joined
    Oct 2009
    Posts
    1

    Please correct the program

    Can somebody please correct my MATLAB program. I am new to matlab and I am taking a course (not matlab) where it requires some matlab.

    the code below should return answer of integral...

    Code:
    % MATLAB Codes for GAUSS QUADRATURE
    % Shakil Ahmed
    clear all
    p=4; %Degree of Polynomial
    n=(p+1)/2; %To find number of Gauss points, ngp
    ngp=round(n)
    b=4; %Upper Limit
    a=0; %Lower Limit
    J=(b-a)/2; %Jacobian
    % get gauss points in the parent element domain [-1, 1] and the corresponding weights
    % function [w,gp] = gauss(ngp)
    if (ngp == 1)
      gp= 0;
      w= 2;
      for nn=1:ngp
        x=((a+b)/2)+(gp(nn)*((b-a)/2))
        %f(nn)=(x^2+1); %Problem 4.6(a)
        % ANSWER IS 25.333
        f(nn)=(x^4 + 2*x^2); %Problem 4.6(b)
        %ANSWER IS 1.73333
        I=[I J*w*f]
        A=sum(I)
       if (ngp == 2)
         gp = [-0.57735027, 0.57735027];
         w = [1, 1];
         for nn=1:ngp
           x=((a+b)/2)+(gp(nn)*((b-a)/2))
           %f(nn)=(x^2+1); %Problem 4.6(a)
           % ANSWER IS 25.333
           f(nn)=(x^4 + 2*x^2); %Problem 4.6(b)
           %ANSWER IS 1.73333
           I=[I J*w*f]
           A=sum(I)
           if (ngp == 3)
             gp = [-0.7745966692, 0.7745966692, 0.0];
             w = [0.5555555556, 0.5555555556, 0.8888888889];
             for nn=1:ngp
               x=((a+b)/2)+(gp(nn)*((b-a)/2))
               %f(nn)=(x^2+1); %Problem 4.6(a)
               % ANSWER IS 25.333
               f(nn)=(x^4 + 2*x^2); %Problem 4.6(b)
               %ANSWER IS 1.73333
               I=[I J*w*f]
               A=sum(I)
             end
           end
         end
    Last edited by CaptainBlack; October 6th 2009 at 01:44 PM.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by shakil View Post
    Can somebody please correct my MATLAB program. I am new to matlab and I am taking a course (not matlab) where it requires some matlab.

    the code below should return answer of integral...

    Code:
    % MATLAB Codes for GAUSS QUADRATURE
    % Shakil Ahmed
    clear all
    p=4; %Degree of Polynomial
    n=(p+1)/2; %To find number of Gauss points, ngp
    ngp=round(n)
    b=4; %Upper Limit
    a=0; %Lower Limit
    J=(b-a)/2; %Jacobian
    % get gauss points in the parent element domain [-1, 1] and the corresponding weights
    % function [w,gp] = gauss(ngp)
    if (ngp == 1)
      gp= 0;
      w= 2;
      for nn=1:ngp
        x=((a+b)/2)+(gp(nn)*((b-a)/2))
        %f(nn)=(x^2+1); %Problem 4.6(a)
        % ANSWER IS 25.333
        f(nn)=(x^4 + 2*x^2); %Problem 4.6(b)
        %ANSWER IS 1.73333
        I=[I J*w*f]
        A=sum(I)
       if (ngp == 2)
         gp = [-0.57735027, 0.57735027];
         w = [1, 1];
         for nn=1:ngp
           x=((a+b)/2)+(gp(nn)*((b-a)/2))
           %f(nn)=(x^2+1); %Problem 4.6(a)
           % ANSWER IS 25.333
           f(nn)=(x^4 + 2*x^2); %Problem 4.6(b)
           %ANSWER IS 1.73333
           I=[I J*w*f]
           A=sum(I)
           if (ngp == 3)
             gp = [-0.7745966692, 0.7745966692, 0.0];
             w = [0.5555555556, 0.5555555556, 0.8888888889];
             for nn=1:ngp
               x=((a+b)/2)+(gp(nn)*((b-a)/2))
               %f(nn)=(x^2+1); %Problem 4.6(a)
               % ANSWER IS 25.333
               f(nn)=(x^4 + 2*x^2); %Problem 4.6(b)
               %ANSWER IS 1.73333
               I=[I J*w*f]
               A=sum(I)
             end
           end
         end
    First close all the blocks opened by "if" or "for".

    Then try executing the code in the debugger.

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Replies: 15
    Last Post: July 29th 2011, 01:39 AM
  2. Fixing The Program (Program Correctness)
    Posted in the Discrete Math Forum
    Replies: 2
    Last Post: March 21st 2009, 02:17 PM
  3. TI Program
    Posted in the Calculators Forum
    Replies: 6
    Last Post: May 10th 2008, 04:40 AM
  4. Need help with program
    Posted in the Math Software Forum
    Replies: 2
    Last Post: February 26th 2008, 12:39 PM
  5. program on TI-84
    Posted in the Calculators Forum
    Replies: 7
    Last Post: August 29th 2006, 06:05 AM

Search Tags


/mathhelpforum @mathhelpforum