Results 1 to 3 of 3

Math Help - Need matlab help!!!

  1. #1
    Newbie
    Joined
    Jan 2010
    Posts
    2

    Need matlab help!!!

    Hi i'm taking an introductory course to matlab at Fau and i'm having a little trouble with my first project. Seeing as how this looks completely foreign to me, I have no clue what to do actually. I was wondering if someone could help me out. This is the project template:

    Develop a computer program in MATLAB that will evaluate the following function for -0.9 ≤ x ≤ 0.9 in steps of 0.1 by:
    A) an arithmetic statement .
    B) by series allowing for as many as 50 terms. However, end adding terms when the last term only affects the 6th place in the answer.
    The function and its series expansion is:

    f(x)= (1+x^2)^.5= 1-1/2x^2 + 1/2 x 3/4x^4 - 1/2 x 3/4 x 5/6x^6 +-....

    Print out a table (to a file) in the following format; use 6 decimal places for f(x).
    X f(x)(by arith stm) f(x)(by series) ]
    -0.9
    -


    -0.8
    -


    -0.7
    -


    - ...
    -


    - ...
    -


    0.7
    -


    0.8
    -


    0.9
    -

    and there should be one last row that says # of terms used in the series

    This program should be written off of these examples...

    Example 2.3
    Code:
    % while1.m
    % Calculation of e^x by a Taylor series using a while loop. The 'input 
    % statement' is used to establish the exponent, x. A 'while loop' is 
    % used in determining the series solution. In this example term(n) is 
    % obtained by multiplying term(n-1) by x and dividing by the index n. 
    clear; clc;
    x=input('enter a value for the exponent x \n');
    n=1; 
    term=1.0; 
    ex=1.0;
    while abs(term) > ex*1.0e-6
        term=term*x/n; 
        ex=ex+term; 
        n=n+1;
        if n > 50
            break;
        end
    end
    disp(x); disp(ex);
    And

    EXAMPLE 2.2
    Code:
    % exB.m
    % The program calculates of e^x by both an arithmetic statement (ex2)
    % and by a Taylor series (ex1), where -0.5< x <0.5. A 'for loop' is
    % used to determine the x values. In the series part of the program,
    % the 'sum' function is used to sum all the terms calculated in the 
    % inner 'for loop'. Fifty terms are used in the series. 
       clear; clc; 
       xmin=-0.5; dx=0.1; 
    % Table headings
       fprintf(' x           ex1         ex2 \n');
       for i=1:11
           x=xmin+(i-1)*dx;
           ex2=exp(x);
           for n=1:50
             term(n)=x^n/factorial(n);
           end   
           ex1=1.0+sum(term);
           fprintf('%5.2f   %10.5f   %10.5f \n',x,ex1,ex2);
       end
    If someone could at least tell me how to go about starting to write this program. I would be very appreciative.


    Thanks,
    stephan

    P.s. I tried to get the appropriate spacings to translate over to this text box but it just wasn't having it.
    Last edited by CaptainBlack; January 29th 2010 at 11:19 AM.
    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 spikek1 View Post
    Hi i'm taking an introductory course to matlab at Fau and i'm having a little trouble with my first project. Seeing as how this looks completely foreign to me, I have no clue what to do actually. I was wondering if someone could help me out. This is the project template:

    Develop a computer program in MATLAB that will evaluate the following function for -0.9 ≤ x ≤ 0.9 in steps of 0.1 by:
    A) an arithmetic statement .
    B) by series allowing for as many as 50 terms. However, end adding terms when the last term only affects the 6th place in the answer.
    The function and its series expansion is:

    f(x)= (1+x^2)^.5= 1-1/2x^2 + 1/2 x 3/4x^4 - 1/2 x 3/4 x 5/6x^6 +-....

    Print out a table (to a file) in the following format; use 6 decimal places for f(x).
    X f(x)(by arith stm) f(x)(by series) ]
    -0.9
    -


    -0.8
    -


    -0.7
    -


    - ...
    -


    - ...
    -


    0.7
    -


    0.8
    -


    0.9
    -

    and there should be one last row that says # of terms used in the series

    This program should be written off of these examples...

    Example 2.3
    Code:
    % while1.m
    % Calculation of e^x by a Taylor series using a while loop. The 'input 
    % statement' is used to establish the exponent, x. A 'while loop' is 
    % used in determining the series solution. In this example term(n) is 
    % obtained by multiplying term(n-1) by x and dividing by the index n. 
    clear; clc;
    x=input('enter a value for the exponent x \n');
    n=1; 
    term=1.0; 
    ex=1.0;
    while abs(term) > ex*1.0e-6
        term=term*x/n; 
        ex=ex+term; 
        n=n+1;
        if n > 50
            break;
        end
    end
    disp(x); disp(ex);
    And

    EXAMPLE 2.2
    Code:
    % exB.m
    % The program calculates of e^x by both an arithmetic statement (ex2)
    % and by a Taylor series (ex1), where -0.5< x <0.5. A 'for loop' is
    % used to determine the x values. In the series part of the program,
    % the 'sum' function is used to sum all the terms calculated in the 
    % inner 'for loop'. Fifty terms are used in the series. 
       clear; clc; 
       xmin=-0.5; dx=0.1; 
    % Table headings
       fprintf(' x           ex1         ex2 \n');
       for i=1:11
           x=xmin+(i-1)*dx;
           ex2=exp(x);
           for n=1:50
             term(n)=x^n/factorial(n);
           end   
           ex1=1.0+sum(term);
           fprintf('%5.2f   %10.5f   %10.5f \n',x,ex1,ex2);
       end
    If someone could at least tell me how to go about starting to write this program. I would be very appreciative.


    Thanks,
    stephan

    P.s. I tried to get the appropriate spacings to translate over to this text box but it just wasn't having it.
    Look at the examples. Load them and single step through in the debugger to get an idea of how they work.

    Then change the calculation of the next term in the series from that in the examples to that for the function you have been asked for.

    CB
    Follow Math Help Forum on Facebook and Google+

  3. #3
    Newbie
    Joined
    Jan 2010
    Posts
    2
    This is pretty much what i was able to write on my own today by comparing what i needed to result in and what the examples show.

    clc; clear;
    xmin=-.9; dx=.1;
    fprintf(' x f(x)(my arith. statement) f(x)(by series) # of terms per series \n');
    fprintf('-------------------------------------------------------------- /n');
    for i=1:19
    x=xmin+(i-1)*dx;
    ex2=(1+x^2)^-.5;
    for n=1:50
    term=x^n/factorial(n);
    sum=sum+term
    if (abs(term) <= 1.00e-6;
    break;
    end;
    end;
    ex1=sum;
    fprintf('%5.2f %10.6f %10.6f \n',x,ex2,ex1);
    end

    I don't know if i did this right... i'm not even sure if it's putting out the results my project is asking me to project... I also know that i need to "print" my results to a file which includes something like --- fid=fopen('output.dat','w'); and so on... but i'm not sure how to incorporate that. I also don't know how to set it up so i list the number of terms/ series in the last column. That part is completly beyond me too. My book is absolutely no help... I wish it was. but i've read the chapter three tiems and i'm still lost. HELP!!
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Matlab help
    Posted in the Math Software Forum
    Replies: 1
    Last Post: February 11th 2010, 03:59 AM
  2. Matlab FFT Help
    Posted in the Math Software Forum
    Replies: 0
    Last Post: June 3rd 2009, 01:48 PM
  3. help about ODE in matlab
    Posted in the Math Software Forum
    Replies: 1
    Last Post: May 31st 2009, 04:10 AM
  4. Matlab
    Posted in the Math Software Forum
    Replies: 0
    Last Post: November 6th 2008, 09:46 AM
  5. Matlab
    Posted in the Calculus Forum
    Replies: 3
    Last Post: August 20th 2008, 11:39 PM

Search Tags


/mathhelpforum @mathhelpforum