Results 1 to 8 of 8

Math Help - Matlab programming error help

  1. #1
    Newbie
    Joined
    Feb 2010
    Posts
    4

    Matlab programming error help

    Hi Guys
    I am trying to find a problem with this program.

    This is the question I was given:

    Using the MATLAB function integ (below) write an m–file to calculate the first 50 pairs of coefficients An and Bn which express a saw-tooth wave as a Fourier Series. Your code should define one period of the saw-tooth wave x(t) and call the function integ to perform the appropriate integration.
    function [area] = integ(x,t)
    % integ is a function which calculates an approximation
    % to the integral of x(t) between the limits t(1) (the
    % first value of the array t) and t(end) (the last value
    % of the array t). It is assumed that the points in t are
    % equally spaced a distance dt apart.
    area=0;
    dt=t(2) – t(1);
    for k=1:length(x)-1,
    area = area + (x(k)+x(k+1))*dt/2
    end;



    This is what I have written:

    Code for 50 coefficients:
    dt =0.0125
    t = [-0.5:dt:0.5];
    s=t;
    y=0;
    T = 1;
    f = 1/T;
    for n = 1:1:50
    x= cos(2*pi*n*f*t);
    xa=s.*x;
    a(n)=2*integ(xa,t);
    z= sin(2*pi*n*f*t);
    xb=s.*z;
    b(n)=2*integ(xb,t);
    end;
    for k= 1:1:50
    y=y + a(k)*(cos(2*pi*k*f*t))+ b(k)*(sin(2*pi*k*f*t));
    end;
    plot (t,y);


    Its giving an error when you run it.

    Can someone help me to fix this please?


    Thankss
    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 killer6008 View Post
    Hi Guys
    I am trying to find a problem with this program.

    This is the question I was given:

    Using the MATLAB function integ (below) write an m–file to calculate the first 50 pairs of coefficients An and Bn which express a saw-tooth wave as a Fourier Series. Your code should define one period of the saw-tooth wave x(t) and call the function integ to perform the appropriate integration.
    function [area] = integ(x,t)
    % integ is a function which calculates an approximation
    % to the integral of x(t) between the limits t(1) (the
    % first value of the array t) and t(end) (the last value
    % of the array t). It is assumed that the points in t are
    % equally spaced a distance dt apart.
    area=0;
    dt=t(2) – t(1);
    for k=1:length(x)-1,
    area = area + (x(k)+x(k+1))*dt/2
    end;



    This is what I have written:

    Code for 50 coefficients:
    dt =0.0125
    t = [-0.5:dt:0.5];
    s=t;
    y=0;
    T = 1;
    f = 1/T;
    for n = 1:1:50
    x= cos(2*pi*n*f*t);
    xa=s.*x;
    a(n)=2*integ(xa,t);
    z= sin(2*pi*n*f*t);
    xb=s.*z;
    b(n)=2*integ(xb,t);
    end;
    for k= 1:1:50
    y=y + a(k)*(cos(2*pi*k*f*t))+ b(k)*(sin(2*pi*k*f*t));
    end;
    plot (t,y);


    Its giving an error when you run it.

    Can someone help me to fix this please?


    Thankss
    What error message?

    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Feb 2010
    Posts
    4
    ??? Undefined function or method 'integ' for input arguments of type 'double'.

    Error in ==> untitled3 at 10
    a(n)=2*integ(xa,t);


    Weird thing is one of my friend has got 2009a matlab student version and it works fine for him and it gives me this error with 2009b full version
    Follow Math Help Forum on Facebook and Google+

  4. #4
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by killer6008 View Post
    ??? Undefined function or method 'integ' for input arguments of type 'double'.

    Error in ==> untitled3 at 10
    a(n)=2*integ(xa,t);


    Weird thing is one of my friend has got 2009a matlab student version and it works fine for him and it gives me this error with 2009b full version
    Is integ in a file called integ.m and on the Matlab path?

    For the purposes of this problem there is no significant difference between 2009a and 2009b (and in general the student version is more complete than the "full" version as you do not have to licence every tool box you need for an extra £600 a whack)

    CB
    Follow Math Help Forum on Facebook and Google+

  5. #5
    Newbie
    Joined
    Feb 2010
    Posts
    4
    I don't know about that file do I need to do something with it??

    All I do is make a new m file paste this coding in there and it gives me an error. Integ is supposed to be an integration function isnt it?

    Thats the same thing my friend does and it works fine for him. It doens't make sense.
    Follow Math Help Forum on Facebook and Google+

  6. #6
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by killer6008 View Post
    I don't know about that file do I need to do something with it??

    All I do is make a new m file paste this coding in there and it gives me an error. Integ is supposed to be an integration function isnt it?

    Thats the same thing my friend does and it works fine for him. It doens't make sense.
    Where is integ.m, that is what directory is it in?

    CB
    Follow Math Help Forum on Facebook and Google+

  7. #7
    Newbie
    Joined
    Feb 2010
    Posts
    4
    what is integ.m?? is that another file? because there is no other file?

    and if you are talking about same file its not saved with integ.m filename
    Follow Math Help Forum on Facebook and Google+

  8. #8
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by killer6008 View Post
    what is integ.m?? is that another file? because there is no other file?

    and if you are talking about same file its not saved with integ.m filename
    So do you have a single file containing both the function integ and the script?

    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. MATLAB error
    Posted in the Math Software Forum
    Replies: 1
    Last Post: March 31st 2010, 11:39 AM
  2. MATLAB linear programming question
    Posted in the Math Software Forum
    Replies: 0
    Last Post: January 31st 2009, 03:08 PM
  3. MATLAB linear programming
    Posted in the Math Software Forum
    Replies: 0
    Last Post: January 31st 2009, 03:02 PM
  4. C++/matlab programming
    Posted in the Math Software Forum
    Replies: 1
    Last Post: January 21st 2008, 11:09 PM
  5. Matlab programming
    Posted in the Math Software Forum
    Replies: 12
    Last Post: December 12th 2007, 03:38 AM

Search Tags


/mathhelpforum @mathhelpforum