Start with the Cody template (reproduced below) Tip for using Cody: Be sure to t
ID: 3857012 • Letter: S
Question
Start with the Cody template (reproduced below) Tip for using Cody: Be sure to test negative input and zero input in your MATLAB before submitting on Cody function [x, ea] = mySqrt (a, es) %calculates squareroot by divide and average (chapra 3.13 & chapra 4.1) % %[x, ea] = mySqrt (a, es) %INPUT a: a scalar real number % es: error stopping criterion, % %OUTPUT x: squareroot of a % ea: the final error, % x = a: %replace this line with your code to solve for the squareroot of x end Compute the absolute error E_t and the relative error e_t for each the following pairs. Use your calculator for "true". true = pi, approximation = 3.14 true = sin(pi/4), approximation = pi/4 true = squareroot 17 approximation = 4.123 Instead of estimating sin(pi/4), use the Maclaurin series expansion of sin(x), shown below, to estimate sin(3 pi/16) accurate to at least 5 sig figs. Use your calculator and not MATLAB. Show the results in a table like the one in Example 4.1 in your book. sin x = sigma^infinity _n=0 (-1)^n/(2n + 1)! x^2n+1 = x - x^3/3! + x^5/5! - middot middot middot for all xExplanation / Answer
function [sqrtx] = sqrRoot(x,tol)
n = 0;
x=0;%initialized variables
if x >=tol %skips all remaining code
return
end
while x <=tol
%code repeated during each loop
x = x+1 %counting code
end
while abs(y^2 - x) > tol
%// Calculate new y from the formula
end
function [sqrtx,n] = sqrRoot(x,tol) %// Change
%// Counts total number of iterations
n = 0;
%// Initialize the previous and current value to the input
sqrtx = x;
sqrtx_prev = x;
%// Until the tolerance has been met...
while abs(sqrtx^2 - x) > tol
%// Compute the next guess of the square root
sqrtx = 0.5*(sqrtx_prev + (x/sqrtx_prev));
%// Increment the counter
n = n + 1;
%// Set for next iteration
sqrtx_prev = sqrtx;
end
>> [sqrtx, n] = sqrRoot(25, 1e-10)
sqrtx =
5
n =
7
>>syms x
>>f=inline(’sin(x)’)
f =
Inline function:
f(x) = sin(x)
>>taylor(f(x),2,0)
ans =
x
>>taylor(f(x),4,0)
ans =
x-1/6*xˆ3
>>taylor(f(x),6,0)
ans =
x-1/6*xˆ3+1/120*xˆ5
>>taylor(f(x),8,0)
ans =
x-1/6*xˆ3+1/120*xˆ5-1/5040*xˆ7
1
>>syms x
>>f=inline(’log(x)’)
f =
Inline function:
f(x) = log(x)
>>taylor(f(x),2,1)
ans =
x-1
>>taylor(f(x),3,1)
ans =
x-1-1/2*(x-1)ˆ2
>>taylor(f(x),4,1)
ans =
x-1-1/2*(x-1)ˆ2+1/3*(x-1)ˆ3
>>taylor(f(x),5,1)
ans =
x-1-1/2*(x-1)ˆ2+1/3*(x-1)ˆ3-1/4*(x-1)ˆ4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.