An IMC student wants to determine an approximation of the square root of 2 and uses continued fractions for it. It is 1 √√2 1+ 1 2+ 2+ 1 2+... Which of the following MATLAB codes cannot be used in order to compute a correct approximation? Code A function [] continued_fraction() % using a for-loop x=2; for k=1:10 x=2+1/(2+1/x); end result=x-1 end Code B function [x] = continued_fraction(n,x) % using a recurrence relation % start for example with: % continued_fraction (10,2) if n>=1 x = continued fraction (n-1,2+1/(2+1/x)); else x=x-1; end end Code C function []=continued_fraction() % using a while-loop and an % anonymous function myfun=@(x) 2+1/(2+1/x); deviation=1; x=2; while abs(deviation)>1e-06 x_new myfun(x); deviation=x_new-x; x=x_new; end result=x-1 end Code D function []=continued_fraction() % using vectors and a separate function x=2; n=10; vec=ones(n,1).*x; vec(2:end)=vec(1:end-1)+... 1./calc frac/vec(1:end-1)); res=max(vec)-1 end function [x_out]=calc_frac{x_in) x_out 2+1./(2+1./x_in); end
An IMC student wants to determine an approximation of the square root of 2 and uses continued fractions for it. It is 1 √√2 1+ 1 2+ 2+ 1 2+... Which of the following MATLAB codes cannot be used in order to compute a correct approximation? Code A function [] continued_fraction() % using a for-loop x=2; for k=1:10 x=2+1/(2+1/x); end result=x-1 end Code B function [x] = continued_fraction(n,x) % using a recurrence relation % start for example with: % continued_fraction (10,2) if n>=1 x = continued fraction (n-1,2+1/(2+1/x)); else x=x-1; end end Code C function []=continued_fraction() % using a while-loop and an % anonymous function myfun=@(x) 2+1/(2+1/x); deviation=1; x=2; while abs(deviation)>1e-06 x_new myfun(x); deviation=x_new-x; x=x_new; end result=x-1 end Code D function []=continued_fraction() % using vectors and a separate function x=2; n=10; vec=ones(n,1).*x; vec(2:end)=vec(1:end-1)+... 1./calc frac/vec(1:end-1)); res=max(vec)-1 end function [x_out]=calc_frac{x_in) x_out 2+1./(2+1./x_in); end
MATLAB: An Introduction with Applications
6th Edition
ISBN:9781119256830
Author:Amos Gilat
Publisher:Amos Gilat
Chapter1: Starting With Matlab
Section: Chapter Questions
Problem 1P
Related questions
Question
![An IMC student wants to determine an approximation of the square root of 2 and uses continued fractions for it. It is
1
√√2 1+
1
2+
2+
1
2+...
Which of the following MATLAB codes cannot be used in order to compute a correct approximation?
Code A
function [] continued_fraction()
% using a for-loop
x=2;
for k=1:10
x=2+1/(2+1/x);
end
result=x-1
end
Code B
function [x] = continued_fraction(n,x)
% using a recurrence relation
% start for example with:
% continued_fraction (10,2)
if n>=1
x = continued fraction (n-1,2+1/(2+1/x));
else
x=x-1;
end
end
Code C
function []=continued_fraction()
% using a while-loop and an
% anonymous function
myfun=@(x) 2+1/(2+1/x);
deviation=1;
x=2;
while abs(deviation)>1e-06
x_new myfun(x);
deviation=x_new-x;
x=x_new;
end
result=x-1
end
Code D
function []=continued_fraction()
% using vectors and a separate function
x=2;
n=10;
vec=ones(n,1).*x;
vec(2:end)=vec(1:end-1)+...
1./calc frac/vec(1:end-1));
res=max(vec)-1
end
function [x_out]=calc_frac{x_in)
x_out 2+1./(2+1./x_in);
end](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F4810b3f8-7174-4e11-ad5b-d1003066dd42%2F2a9034ae-4868-4a9a-aeb5-66c1564059a1%2Ffyycvcb_processed.png&w=3840&q=75)
Transcribed Image Text:An IMC student wants to determine an approximation of the square root of 2 and uses continued fractions for it. It is
1
√√2 1+
1
2+
2+
1
2+...
Which of the following MATLAB codes cannot be used in order to compute a correct approximation?
Code A
function [] continued_fraction()
% using a for-loop
x=2;
for k=1:10
x=2+1/(2+1/x);
end
result=x-1
end
Code B
function [x] = continued_fraction(n,x)
% using a recurrence relation
% start for example with:
% continued_fraction (10,2)
if n>=1
x = continued fraction (n-1,2+1/(2+1/x));
else
x=x-1;
end
end
Code C
function []=continued_fraction()
% using a while-loop and an
% anonymous function
myfun=@(x) 2+1/(2+1/x);
deviation=1;
x=2;
while abs(deviation)>1e-06
x_new myfun(x);
deviation=x_new-x;
x=x_new;
end
result=x-1
end
Code D
function []=continued_fraction()
% using vectors and a separate function
x=2;
n=10;
vec=ones(n,1).*x;
vec(2:end)=vec(1:end-1)+...
1./calc frac/vec(1:end-1));
res=max(vec)-1
end
function [x_out]=calc_frac{x_in)
x_out 2+1./(2+1./x_in);
end
Expert Solution

This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 2 steps

Recommended textbooks for you

MATLAB: An Introduction with Applications
Statistics
ISBN:
9781119256830
Author:
Amos Gilat
Publisher:
John Wiley & Sons Inc

Probability and Statistics for Engineering and th…
Statistics
ISBN:
9781305251809
Author:
Jay L. Devore
Publisher:
Cengage Learning

Statistics for The Behavioral Sciences (MindTap C…
Statistics
ISBN:
9781305504912
Author:
Frederick J Gravetter, Larry B. Wallnau
Publisher:
Cengage Learning

MATLAB: An Introduction with Applications
Statistics
ISBN:
9781119256830
Author:
Amos Gilat
Publisher:
John Wiley & Sons Inc

Probability and Statistics for Engineering and th…
Statistics
ISBN:
9781305251809
Author:
Jay L. Devore
Publisher:
Cengage Learning

Statistics for The Behavioral Sciences (MindTap C…
Statistics
ISBN:
9781305504912
Author:
Frederick J Gravetter, Larry B. Wallnau
Publisher:
Cengage Learning

Elementary Statistics: Picturing the World (7th E…
Statistics
ISBN:
9780134683416
Author:
Ron Larson, Betsy Farber
Publisher:
PEARSON

The Basic Practice of Statistics
Statistics
ISBN:
9781319042578
Author:
David S. Moore, William I. Notz, Michael A. Fligner
Publisher:
W. H. Freeman

Introduction to the Practice of Statistics
Statistics
ISBN:
9781319013387
Author:
David S. Moore, George P. McCabe, Bruce A. Craig
Publisher:
W. H. Freeman