# Math Help - Need matlab help!!!

1. ## 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;
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.

2. Originally Posted by spikek1
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;
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

3. 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!!