Develop a computer program in MATLAB that will evaluate the following function f
ID: 3809449 • Letter: D
Question
Develop a computer program in MATLAB that will evaluate the following function for 0.9 x 0.9 in steps of 0.1 by:
1. An arithmetic statement.
2. A series allowing for as many as 50 terms. However, stop adding terms when the last term only affects the sixth decimal place in the answer.
The function and its series expansion, valid for x2 1, is
f(x) = (1+x2)-1/2 = 1 - (1/2*x2) + ((1*3)/(2*4)*x4) - ((1*3*5)/(2*4*6)*x6) + ((1*3*5*7)/(2*4*6*8)*x8) - ... + ...
Print out a table in the format below:
x f(x) (arithmetic statement) f(x) (by series) No. of terms used in the series
-0.9
-0.8
-0.7
........
0.7
0.8
0.9
Explanation / Answer
function [sum, count] = calc(x)
n = 2;
sum = 0;
lastTerm = 1;
count = 1;
while (lastTerm > 0.1e-6 && count<50) % max count 50 or last term does not effect 6th decipal place after point
sum = sum + (-1)^count * lastTerm;
lastTerm = 1;
for i =1:n
if mod(i,2)==1
lastTerm = lastTerm * i; %multiply odd number
else
lastTerm = lastTerm / i; %divide even numbers
end
end
lastTerm = lastTerm * x^n;
n = n + 2;
count = count + 1;
end %end of while
end %end of function
for i = -0.9:0.1:0.9
[x,y] = calc(i);
z = (1+i^2)^(-1/2);
printf('%d %d %d %d ',i,z,x,y);
end
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.