0% found this document useful (0 votes)
60 views4 pages

Assignment4 (hw4)

The document contains examples of MATLAB code for calculating the variance of a dataset incrementally, finding the root of a function using iterative methods, approximating the complete elliptic integral of the first kind using the arithmetic-geometric mean algorithm, generating a triangular matrix, plotting the logistic map function iteratively and using a while loop, and plotting the same function using a for loop. Each example includes the MATLAB code and output.

Uploaded by

Hemil Chauhan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views4 pages

Assignment4 (hw4)

The document contains examples of MATLAB code for calculating the variance of a dataset incrementally, finding the root of a function using iterative methods, approximating the complete elliptic integral of the first kind using the arithmetic-geometric mean algorithm, generating a triangular matrix, plotting the logistic map function iteratively and using a while loop, and plotting the same function using a for loop. Each example includes the MATLAB code and output.

Uploaded by

Hemil Chauhan
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

%% example 4.

2
dat = [45 38 47 41 35 43]; L = length(dat);
vvar = zeros(L,1);
for n = 2:L
vvar(n) = var(dat(1:n));
end
disp([repmat('var(n=', L-1, 1) int2str((2:L)') repmat('): ', L-1,1)
num2str(vvar(2:L),4)])

ans
var(n=2): 24.5
var(n=3): 22.33
var(n=4): 16.25
var(n=5): 24.2
var(n=6): 19.9

%% example 4.4
xold = 1.5; fx = 1;
ct = 0;
while fx>1e-8
xnew = xold-(cos(xold)+sin(xold))/(-sin(xold)+cos(xold));
fx = abs(cos(xnew)+sin(xnew));
ct = ct+1;
xold = xnew;
end
disp(['At x = ' num2str(xold,8) ', f(x) = ' num2str(fx) ' after ' int2str(ct)
' iterations'])

ans
At x = 2.3561945, f(x) = 1.1102e-16 after 4 iterations
%% example 4.6
alp = pi/4;
tl = 1e-5;
ao = 1;
bo = cos(alp);
while abs(co)>tl
an = (ao+bo)/2;
bn = sqrt(ao*bo);
co = (ao-bo)/2;
ao = an;
bo = bn;
end
format long e
Kalp = pi/ao/2
K = ellipke(sin(alp)^2)

co = sin(alp);

format short

ans
Kalp =

1.854074677301372e+00

K=

1.854074677301372e+00
%% example 4.8
N = input('Enter a positive integer < 10: ');
h = zeros(N,N);
for m = 1:N
for n = 1:N
if (n+m-1) <= N
h(n, m) = n+m-1;
end
end
end
disp(h)

Ans
Enter a positive integer < 10: 4
1

%% example 4.11
x(1) = 0;
n = 201;
x = zeros(n,1);
for k = 2:n
x(k) = x(k-1)^2+0.25;
end
plot(0:5:n-1, x(1:5:n), 'ks')
m = 1;
xw(1) = 0;
xw = zeros(201, 1);
while m<201
xw(m+1) = xw(m)^2+0.25;
m = m+1;
end
figure(2)
plot(0:5:m-1, xw(1:5:m), 'ks')

ans

You might also like