@
),
you have to create a file and save it. For example, you have to create the file a=1e8
, the smaller root computed from (a-sqrt(a^2-4))/2
is
7.450580596923828e-09
(using the default double precision in MATLAB), which is
wrong in the first digit. In stead, we should use the equivalent formula
quadroot
by continuing the following block of code (save it as a matlab file "quadroot.m"). You can assume that the input argument function y=quadroot(a)
%QUADROOT Roots of the quadratic function x^2-a*x+1=0
% quadroot(a) returns the two roots of the quadratic function x^2-a*x+1=0
% such that both roots are accurate even if the absolute value of a is large
x = (x^2+2)/2/x;
, instead of saving all the history. Which iteration
is faster (you can counter the number of steps, by adding the counter by one inside each iteration)?
function isleap(year)This function can be written in alternative ways to make it clear, using the following figure (the shaded set indicates leap year).
%% for the input year, determine whether this is a leap year
if mod(year,4)~=0
disp('This is not a leap year');
elseif mod(year,100)~=0
disp('This is a leap year');
elseif mod(year,400)==0
disp('This is a leap year');
else
disp('This is not a leap year');
end
if
statements as follow. Replace
% insert the display here
by
either disp('This is not a leap year');
or disp('This is a leap year');
.
function isleap1(year)
%% for the input year, determine whether this is a leap year
% using nested "if" statements
% save this file as "isleap1.m"
if (mod(year,4)==0)
if (mod(year,100)==0)
if (mod(year,400)==0)
% insert the display here
else
% insert the display here
end
else
% insert the display here
end
else
% insert the display here
end
&
and/or logical "or" |
. For example, there are two
white regions representing non-leap years in the figure. The outer one
is characterized by mod(year,4)~=0
, and
the inner one by (mod(year,100)==0 & mod(year,400)~=0)
.
Therefore, the corresponding function becomes
function isleap2(year)Now write a function using some logical statements that first characterize the two shaded region in the figure. You can just modify the following code (add some statements inside the bracket following
%% for the input year, determine whether this is a leap year
% using logical "and" and "or"
% save this file as "isleap2.m"
if ((mod(year,4)~=0) | (mod(year,100)==0 & mod(year,400)~=0))
disp('This is not a leap year');
else
disp('This is a leap year');
end
if
:
function isleap3(year)
%% for the input year, determine whether this is a leap year
% using logical "and" and "or"
% save this file as "isleap3.m"
if ()
disp('This is a leap year');
else
disp('This is not a leap year');
end
number_of_tiles(n,k)
returns the number of tiles in k-th row. In the following figure,
number_of_tiles(8,2)
and
number_of_tiles(8,6)
. How is the output related to floor
the integer part of a decimal number, of course there are other related commands like
ceil
and round
)
function t = number_of_tiles(n,k)In fact, the function is so short, you can write it more conveniently as an anonymous function
%% for the input integer radius n, output the number of tiles in k-th row
% output a vector, if the input k is also a vector (we would like to call "number_of_tiles(n,1:n)" ),
% so use vector operator if possible
number_of_tiles = @(n,k) % complete this line
number_of_tiles(n,1:n)
)
is about the area of the quarter circle format long;Increase the value of
n = 100; [4*sum(number_of_tiles(n,1:n))/n^2; pi]
function counter = collatz(N)You can test your function, for the following values: (1) the output of
%% For input positive integer N, return of the number of steps, such that "1" first appears
counter = 0; % the initial value
n = N;
while (n ~= 1)
% complete this part, perform one step of the iteration on n, and increase the counter by one
end
collatz(1)
should be 0
; (2) the output of
collatz(2)
should be 1
; (3) the output of collatz(3)
should be 7
. Some small numbers may take many iterations to terminate. For example, the output for
collatz(27)
should be larger than 100.