Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

MATLAB Programming for Numerical Computations Jan–March 2017

Assignment for Module–1


Due on: This assignment does not have a due date

INSTRUCTIONS

• Submit your work on the course website ( https://onlinecourses.nptel.ac.in/noc16_ch01/ )


• Please visit the help section of the course webpage to know exactly how to submit
• Please follow all the instructions carefully
• This assignment is not graded. It is intended to get acquainted with submission portal.

1. DEMO PROBLEM

For 𝑥 in the range x=[0:0.1:1], calculate 𝑓 𝑥 = 𝑥𝑒 %&' . Report the value of 𝑥 at


which 𝑓 𝑥 is maximum.
1. Report the value of 𝑥 for which the above function value is the highest

2. VECTOR SUMMATION

Given a vector x, calculate the following:


oddSum = 𝑥 1 + 𝑥 3 + 𝑥 5 + ⋯
funVal = 𝑥 1 − 𝑥 2 + 𝑥 3 − 𝑥 4 + ⋯
Your should create a function vectorSum that takes one input vector x, and returns two
scalars, oddSum and funVal. Also update these results on the module assessment page for:
x=[0.81 0.91 0.12 0.91 0.63 0.09 0.28].
x=[4 5 0 3].
2–5. Report the value of oddSum and funVal for the two vectors x given above

3. SALARY PROBLEM

Two friends A and B start with initial salaries of 1 unit and 1.25 unit, respectively. At the
end of each year, they get a raise of 6% and 2% respectively. Write a MATLAB code that uses
either a for or while loop to calculate annual salaries of the two until the year when A’s
salary exceeds that of B’s. Report the value of the earliest year 𝑛 when A’s salary exceeds B’s.
When the program ends, A and B should be a 𝑛-dimensional vectors, containing their
salaries in the respective years.
6. Report the value of n for which the salary of A first exceeds that of B.

1
MATLAB Programming for Numerical Computations Jan–March 2017

4. PLOTTING

Define vector t=[0:0.04:4];. Define vertical location of the ball using equation
𝑦 𝑡 = 𝑣4 𝑡 − 0.5 𝑔𝑡 & , as was done in video lectures on Loops and Execution Control. The
values to use are 𝑣9 = 20 and 𝑔 = 9.8. Both t and y are 101-dimensional vectors.
We now wish to do the same plot in a different way. The command plot(t,y,'-bo')
gives blue line with data-points shown as open circles. Since the data is closely spaced, this
plot looks cluttered.
Instead, plot the same graph such that the blue line is as before, but the circles are shown
only at 0.4 seconds interval (i.e., line goes through all 101 points, but symbols are shown for
11 points only). The plot should look like the one below:

Hint: You will have to plot the same data twice: using '-b' to plot the line, and using 'bo'
to plot the symbols only.
If you repeat the problem for t=[0:0.04:3]; keeping everything else the same, how
many symbols are plotted on the figure? Please enter this value in assessment page
7. Report the number of symbols on the plot, when t=[0:0.04:3];

2
1)function f=func(x)
f=x.*exp(-2*x);

func(x)

ans =

Columns 1 through 7

0 0.0819 0.1341 0.1646 0.1797 0.1839 0.1807

Columns 8 through 11

0.1726 0.1615 0.1488 0.1353

2)x=[0.81 0.91 0.12 0.91 0.63 0.09 0.28];


n=7;
for i=1:2:n
odd1(i)=x(i);
end
for j=2:2:n
eve1(j)=x(j);
end
funval=sum(odd1)-sum(eve1);
oddsum=sum(odd1);
funval =

-0.0700

>> oddsum

oddsum =

1.8400
3)salA=1;
salB=1.25;
i=1;
while(salA<salB)
incA=salA*0.06;
incB=salB*0.02;
salA=salA+incA;
salB=salB+incB;
A(i)=salA;
B(i)=salB;
i=i+1;
end
fprintf('\n salary of A will exceed salary of B in %d th year',i);

4)Refer to lecture1.3 & change values accordingly


MATLAB Programming for Numerical Computations Jan–March 2017

Assignment for Module–2


Due on: Wednesday, 8th Feb 2017

INSTRUCTIONS

• Submit your work on the course website (https://onlinecourses.nptel.ac.in/noc17_ge02/)


• Please follow all the instructions carefully and submit before the deadline
• Please use MATLAB templates provided for Problem 2
• You do not need to upload your MATLAB files.

1. SMALLEST ERROR FOR DIFFERENTIATION

Use the “three-point forward difference formula” for finding 𝑓 " 𝑥 for tan'( 𝑥 at 𝑥 = 1
−𝑓 𝑥 + 2ℎ + 4𝑓 𝑥 + ℎ − 3𝑓 𝑥
𝑓" 𝑥 =
2ℎ
For value of 𝑥 = 1, perform the above for various different values of the step-size
ℎ = 10'2 10'3 ⋯ 10'(5 . For each h, compare with the true value 𝑓 " 𝑥 = 1 + 𝑥 2 '(

to compute error, err, as absolute difference between the true and approximate values.
1, 2. Report value of h at which error is minimum. Also report this minimum err value

2. MACLAURIN SERIES FOR HYPERBOLIC FUNCTIONS

Download the file maclaurinCosh.m and use it as a template for this problem.
In this problem, you will use MacLaurin Series until the n-th order terms to compute
approximations of cosh (hyperbolic cosine) function, given by:
𝑥2 𝑥;
cosh 𝑥 = 1 + + + ⋯
2! 4!
In the template file, you will find vector:
𝑥2 𝑥3 𝑥; 𝑥>
vec = 1 𝑥 ⋯
2! 3! 4! 𝑛!
pre-computed for you. Please use the vector vec to compute approximate values of cosh 𝑥 .
Your function should return coshVal, while taking the values of x and n as inputs. Report the
following results:
3. Results on running: coshVal = maclaurinCosh(1.5,4)
4. Results on running: coshVal = maclaurinCosh(3,9)

1
MATLAB Programming for Numerical Computations Jan–March 2017

3. A NON-CONVERGENT INFINITE SERIES

All the examples in the course videos have been about convergent series. In this example,
you will code a series that does not converge:
1 1 1
𝑆 = 1 + + + ⋯+
2 3 𝑛
Write a MATLAB code that takes in the value n and computes the above series sum S.
Please report the value S for various cases, n = 500, 1000, 5000 and 10000.
5–8. Report the values of S for different values of n: 500, 1000, 5000 and 10000.

4. MULTI-STAGE TAYLOR’S SERIES FOR LOG

We used infinite Taylor’s series in Problem 2 to compute cosh(x). In this problem, we


will instead adopt a multi-stage strategy to calculate ln 1 + 𝑥 . Starting with ln 1 = 0, we
will take n steps to calculate ln 1 + 𝑛ℎ . Use step-size h = 0.1.
We will use the property of single-step Taylor’s series1 for this purpose. Let us represent
𝑦( = ln 1 , 𝑦2 = ln 1 + ℎ , 𝑦3 = ln 1 + 2ℎ , …, 𝑦>D( = ln 1 + 𝑛ℎ

At each stage, the next value is:



𝑦ED( = 𝑦E +
1+ 𝑖−1 ℎ
Starting with 𝑦( = 0, we will compute 𝑦2 ; we will use 𝑦2 to compute 𝑦3 and so on…
ℎ ℎ
For example, 𝑦2 = 𝑦( + , 𝑦3 = 𝑦2 + , …
1 1+ℎ

This is repeated multiple times until we reach ln 1.5 = ln 1 + 5ℎ and ln 5 =


ln 1 + 40ℎ . Use the above procedure to compute ln 1.5 and ln 5 by taking 𝑛 = 5 and 𝑛 =
40 steps respectively:
9. Report the approximate value of ln(1.5) computed in 5 stages, using ℎ = 0.1
10. Report the approximate value of ln(5) computed in 40 stages, using ℎ = 0.1

1
For those who are curious about the derivation,
ℎ ℎ ℎ
ln 1 + 𝑥 + ℎ ≈ ln 1 + 𝑥 + ⇒ 𝑦ED( ≈ 𝑦E + = 𝑦E +
1+𝑥 1+𝑥 1+ 𝑖−1 ℎ

2
Assignment for module 2 Due date-8/2/17 A.A.Joshi

Problem no 1

x=1;%%calculating derivative

trueval=1/(1+x^2);

for i=2:10
h=10^(-i);
approxval=(-atan(x+2*h)+4*atan(x+h)-3*atan(x))/(2*h);
err=abs(trueval-approxval)
hAll(i)=h;
errAll(i)=err;
end
%%plot err vs stepsize
hAll
errAll
loglog(hAll,errAll)

output

1.66632285854007e-05 at 10^-2

1.66666513479186e-07 at 10^-3

1.66649982702438e-09 at 10^-4

3.27560201185406e-12 at 10^-5

1.52155621435668e-10 at 10^-6

1.40215661303955e-09 at 10^-7

3.03873548546108e-09 at 10^-8

4.13701854995452e-08 at 10^-9

4.13701854995452e-08 at 10^-10
problem no-2

function [coshVal] = maclaurinCosh(x,n)


% Template file for Assignment-2
% Compute approximation of cosh(x) using MacLaurin
% Series upto the n-th order term (x^n/n!)
% ----- DO NOT EDIT THIS PART OF THE CODE -----
numerator = x.^[1:n];
denom = cumprod(1:n);
vec = [1, numerator./denom];
% ----- DO NOT EDIT ANYTHING ABOVE THIS LINE -----
% PLEASE USE "vec" for your further calculations
% ---- YOU MAY START EDITING THE FUNCTION NOW ----
temp= numerator./denom;
for i=2:2:n
eve1(i)=temp(i);
end
eve1
k=[1,eve1]
coshVal=sum(k);
end

output
coshVal = maclaurinCosh(1.5,4)
eve1 = 0 1.1250 0 0.2109

k= 1.0000 0 1.1250 0 0.2109

coshVal = 2.3359
>> coshVal = maclaurinCosh(3,9)

eve1 = 0 4.5000 0 3.3750 0 1.0125 0 0.1627

k = 1.0000 0 4.5000 0 3.3750 0 1.0125 0 0.1627

coshVal = 10.0502
Problem no-3

function [s] = nonconvergent(n)


vec=[1:n];
temp = 1./vec;
s= sum(temp)

output

nonconvergent(500)
s = 6.7928

nonconvergent(1000)
s = 7.4855

nonconvergent(5000)

s = 9.0945

nonconvergent(10000)

s = 9.7876
Problem no-4

function [y] = multistepTaylor(n)


y0=0;
h=0.1;
for i=1:n
y=y0+(h/(1+(i-1)*h));
y0=y;
end
y

output

multistepTaylor(5)

y = 0.4226

multistepTaylor(40)

y = 1.6502
MATLAB Programming for Numerical Computations Jan–March 2017

Assignment for Module–3


Due on: Wednesday, 15th Feb 2017

INSTRUCTIONS

• Submit your work on the course website (https://onlinecourses.nptel.ac.in/noc17_ge02/)


• Please follow all the instructions carefully and submit before the deadline
• You may use the uploaded template file hw3fun.m for Problem 2
• You do not need to upload your MATLAB files.

1. MULTIPLE APPLICATIONS OF TRAPEZOIDAL RULE (2 point)

Use multiple applications of trapezoidal rule to evaluate the value of integral:


)
𝐼= 𝑥 $ 𝑒 & d𝑥
*

Use n = 5, 25 and 50 intervals to calculate the integral I using trapezoidal rule.


1–3. Report the integral using 5, 25 and 50 intervals with the trapezoidal rule
The true solution is 𝐼+,-. = 𝑒 ) − 2 . Calculate the error, 𝐼1234 − 𝐼 , in using trapezoidal rule.
4. Please report the error using n = 50 intervals of the trapezoidal rule

2. NUMERICAL INTEGRATION USING quad

Please use the function hw3fun.m uploaded on the course website for this problem
We will generalize Problem-1 and use MATLAB function quad to compute the integral:
)
𝐼= 𝑥 $ 𝑒 5& d𝑥
*

Note that the function being integrated, 𝑓 𝑥 = 𝑥 $ 𝑒 5& , also has a parameter a.
A function file hw3fun.m is uploaded to the course website. The function
f=hw3fun(x,a) is called using variable x and parameter a as the two inputs.
Part 2.1: Find the integral I when 𝑎 = 1. Note that this is the same as Problem-1.
Compute the error in using quad as was done in Problem 1.
5. Please report the numerical value of integral I using quad function.
6. Please report the error in computing I using quad function
) $ $&
Part 2.2: Use quad to compute the integral *
𝑥 𝑒 d𝑥 . Note that a = 2 in this problem.
7. Please report the value of integral I using quad function with a = 2.

1
MATLAB Programming for Numerical Computations Jan–March 2017

3. CALCULATION FOR SOLAR ENERGY

The solar heat flux, q, was measured at various times during the day. We wish to calculate
the total heat absorbed on a 5 m2 solar panel during a 12-hour period from 6 am to 6 pm from
the heat flux data given below.
Time 6 am 7 am 8 am 9 am 10 am 11am 12 1 pm 2 pm 3 pm 4 pm 5 pm 6 pm
q 230 425 620 780 910 970 985 990 930 860 765 610 410
The total solar heat absorbed by the panel is given by
1>
𝐻=𝜂 𝑞𝐴 d𝑡
1?

Use multiple applications of the Simpson’s 1/3rd rule to compute the net heat absorbed, H, if
the efficiency of the collector is 𝜂 = 0.4, area 𝐴 = 5 m$ and q is as specified in the table above.
8. Report the total energy absorbed in the above period using Simpson’s 1/3rd rule

4. VELOCITY PROFILE FOR A FLUID FLOWING ON AN INCLINE

A fluid is flowing down an inclined plane. The velocity computed at various locations
from the free-edge of the fluid is given in the table:
𝑦 0 0.01 0.02 0.03 0.04 0.05
𝑣 0.30 0.288 0.252 0.192 0.108 0
GH
Calculate the velocity gradient at various locations in the fluid.
GI

Use central difference for internal locations, forward difference formula at the first node and
backward difference formula at 𝑦 = 0.05.
9. Report the gradient at 𝑦 = 0 using forward difference formula
10. Report the gradient at 𝑦 = 0.02 using central difference formula
11. Report the gradient at 𝑦 = 0.05 using the backward difference formula
11 2
2
33 4
4
55
6 6

2
Assignment Module no-3
Example No-1
function fval=example1(x)
fval=x.^2.*exp(x);
end

%script file for output name it multistepintegral.m


a=0;
b=1;
trueval=exp(1)-2;
n=50;%just change value of n you required
%Trapezoidal rule(multiple)
h=(b-a)/n;
xvec =a:h:b;
fvec=example1(xvec);
%using multiple interval
Interval=zeros(n,1);
for i=1:n
I_interval(i)=h/2*(fvec(i)+fvec(i+1));
end
I_Trap1=sum(I_interval);
err1=abs(trueval-I_Trap1);
%using direct formula
I_Trap2=h/2*(fvec(1)+2*sum(fvec(2:n))+fvec(n+1));
err2=abs(trueval-I_Trap2);
%Using MATLAB Trapz function
I_trap3=trapz(xvec,fvec);
err3=abs(trueval-I_trap3);
%Display results
disp('By using trapezoidal rule multiple times');
disp(I_Trap1);
disp(['For h=',num2str(h), ',Error=',num2str(err1)]);
disp('by using multiple interval');
disp(I_Trap2);
disp(['For h=',num2str(h), ',Error=',num2str(err2)]);
disp('using trapz function');
disp(I_trap3)
disp('Error using trapz')
disp(err3)

page 1:Incredible1
output (n=5)

multistepintegral

By using trapezoidal rule multiple times 0.7454

For h=0.2,Error=0.027118

by using multiple interval

0.7454…………………………………………….(1)

For h=0.2,Error=0.027118

using trapz function

0.7454

Error using trapz

0.0271

output(n=25)

>> multistepintegral

By using trapezoidal rule multiple times

0.7194………………………………………………………………..(2)

For h=0.04,Error=0.0010872

by using multiple interval

0.7194

For h=0.04,Error=0.0010872

using trapz function

0.7194

Error using trapz

0.0011

output(n=50)

>> multistepintegral

page 2:Incredible1
By using trapezoidal rule multiple times

0.7186

For h=0.02,Error=0.00027182……….(3&4)

by using multiple interval

0.7186

For h=0.02,Error=0.00027182

using trapz function

0.7186

Error using trapz

2.7182e-04

Exampl no-2

5 & 6)

%download hw3fun.m & prepare mod2example2.m script file as follows and run

a=0;

b=1;

trueval=exp(1)-2;

%using MATLAB quad function

Q = quad(@(x) hw3fun(x,1),0,1)

err=abs(Q-trueval)

output

Q = 0.7183………….(5)

err = 4.1583e-09…………………(6)

%Just change Q = quad(@(x) hw3fun(x,2),0,1) in above code

Q = 1.5973…………………………….(7)

page 3:Incredible1
Example3

%Script file as following

a=0;

b=12;

t=0:1:12;

fvec=[230 425 620 780 910 970 985 990 930 860 765 610 410];

n=12;

h=(b-a)/n;

%simpson 1/3rd rule used multiple times

I_simp=h/3*(fvec(1)+4*sum(fvec(1:2:n))+2*sum(fvec(2:2:n))+fvec(n+1));

I_simp=0.4*5*I_simp; %Multiply by efficiency & area

disp(I_simp)

otput

1.8447e+04

==18447Watt………………………………(8)

page 4:Incredible1
Example no-4
% script file mod3Example4.m
t=0:0.01:0.05;
v=[.3 .288 .252 .192 .108 0];
h=0.01;
%Forward dif
fwdDiff=(v(2)-v(1))./h;
disp(['forward difference at t=0: ',num2str(fwdDiff)]);
%Centraldiff
ctrDiff=(v(4)-v(2))./(2*h);
disp(['central difference at t=0.02: ',num2str(ctrDiff)]);
%Backward diff
bkDiff=(v(6)-v(5))./h;
disp(['backward difference at t=0.05: ',num2str(bkDiff)]);

output

>> mod2Example4

forward difference at t=0 : -1.2 …………………………..(9)

central difference at t=0.02 : -4.8 …………………………….(10)

backward difference at t=0.05 : -10.8 …………………………….(11)

page 5:Incredible1
MATLAB Programming for Numerical Computations Jan–March 2017

Assignment for Module– 4


Due on: Wednesday, February 22

INSTRUCTIONS

• Submit your work on the course website (https://onlinecourses.nptel.ac.in/noc17_ge02/)


• Please follow all the instructions carefully and submit before the deadline.
• Please use MATLAB templates provided for Problems 2 and 3.
• You do not need to upload your MATLAB files.

1. GAUSS–SIEDEL METHOD (2 POINTS)

Please solve the following set of equations using Gauss-Siedel method:


10x1 – 2 x2 – x3 – x4 = 3
–2 x1 + 10 x2 – x3 – x4 = 15
– x1 – x2 + 10 x3 – 2 x4 = 27
– x1 – x2 – 2 x3 + 10 x4 = –9
Using an initial guess as: x = [0 0 0 0] T, perform 4 iterations and 8 iterations of Gauss-
Siedel method. Please report the absolute error at the end of 4th and 8th iteration. The iteration
error is max(abs(xn – xn-1)).
[1, 2] Report the errors obtained after 4th and 8th iteration. Please don’t report the
solution for the equations. Please report the iteration errors only.

2. TRI-DIAGONAL MATRIX ALGORITHM (2 POINTS)

The TDMA solver myTDMA.m has to be used to solve the problem.


Using the Thomas Algorithm, please solve the following equations:
é1 9 0 0 0ù é x1 ù é5 ù
ê2
ê 3 1 0 0úú êê x 2úú ê 3ú
ê ú
ê0 4 5 2 0 ú ê x3 ú = ê - 1ú
ê úê ú ê ú
ê0 0 3 6 7 ú ê x 4ú ê9 ú
êë0 0 0 8 7 úû êë x5 úû êë- 7úû

[3–7] Report the values of x1, x2, x3, x4 and x5.

1
MATLAB Programming for Numerical Computations Jan–March 2017

3. LU DECOMPOSITION (2 POINTS)

The LU decomposition code myLUcode.m from video lectures is uploaded. You have
the option of starting with this code, or completely using your own new code.
Write the following set of equations in the form of AX = B and perform LU
Decomposition for the matrix A. Given the following set of equations.
8 x1 – 2 x2 + 3 x3 + 4 x4 = 1
2 x1 + 5 x2 – 7 x3 + x4 = 8
7 x1 – x2 + 2 x4 = -5
9 x1 + 7 x2 – 3 x3 + 2 x4 = 7
[8–11] Please report the 1st row of U matrix.

4. APPLICATION OF LINEAR ALGEBRA (2 POINTS)

Happy Hens Organic Farm prepares a special feed which is a mixture of corn, horse
gram and Chick peas. Corn cost ₹ 40 per kg, Horse gram cost ₹ 50 per kg and chick peas
cost ₹ 90 per kg. The special feed should be made such that the weight of corn is twice that
of horse gram. How many kilograms of chick peas should be used to produce the following
amounts of special feed at the mentioned cost?
a) 140 kg at ₹ 60 per kg.
b) 100 kg at ₹ 76 per kg.
c) 125 kg at ₹ 62 per kg.

[12–14] Please report the kilograms of chick peas required for the cases a, b, c. Use any
of the mentioned methods to solve the system of equations.

2
1

Assignment Module-4 Answer key simultaneous equations


Problem no-1

A=[ 10 -2 -1 -1;-2 10 -1 -1;-1 -1 10 -2;-1 -1 -2 10];


b=[3; 15; 27; -9];
Ab=[A b];
As=Ab;
n=4;
x=zeros(n,1);
err=zeros(n,1);
for iter=1:25
for k=1:n
xold=x(k);
num=As(k,end)-As(k,1:k-1)*x(1:k-1)-As(k,k+1:n)*x(k+1:n);
x(k)=num/As(k,k);
err(k)=max(abs(x(k)-xold));
end
disp(['Iter=',num2str(iter),';Error=',num2str(max(err))])
end

output=======

Iter=1;Error=2.886

Iter=2;Error=0.58692

Iter=3;Error=0.096721

Iter=4;Error=0.013165…………………………………………………(1)

Iter=5;Error=0.0026215

Iter=6;Error=0.00047008

Iter=7;Error=8.4499e-05

Iter=8;Error=1.5283e-05------------------------------------------(2)

Iter=9;Error=2.7587e-06

Iter=10;Error=4.9806e-07

Iter=11;Error=8.9926e-08

incredible1 Page 1
2

Iter=12;Error=1.6236e-08

Iter=13;Error=2.9314e-09

Iter=14;Error=5.2926e-10

Iter=15;Error=9.5558e-11

Iter=16;Error=1.7253e-11

Iter=17;Error=3.1151e-12

Iter=18;Error=5.6233e-13

Iter=19;Error=1.0159e-13

Iter=20;Error=1.8319e-14

Iter=21;Error=3.4417e-15

Iter=22;Error=4.4409e-16

Iter=23;Error=2.2204e-16

Iter=24;Error=0

Iter=25;Error=0

Problem no-2

-0.1532

0.5726

1.5887

-5.6169

5.4194

incredible1 Page 2
3

Problem no-3

% LU Decomposition using Naive Gauss Elimination


A = [8 -2 3 4;2 5 -7 1;7 -1 0 2;9 7 -3 2];
b = [1;8;-5;7];

%% Gauss Elimination
% Get augmented matrix
Ab = [A, b];
n = length(A);
L = eye(n);
% With A(1,1) as pivot Element
for i = 2:4
alpha = Ab(i,1)/Ab(1,1);
L(i,1) = alpha;
Ab(i,:) = Ab(i,:) - alpha*Ab(1,:);
end
% With A(2,2) as pivot Element
i = 3;
alpha = Ab(i,2)/Ab(2,2);
L(i,2) = alpha;
Ab(i,:) = Ab(i,:) - alpha*Ab(2,:);
i=4;
alpha = Ab(i,2)/Ab(2,2);
L(i,2) = alpha;
Ab(i,:) = Ab(i,:) - alpha*Ab(2,:);
% With A(3,3) as pivot Element
i=4;
alpha = Ab(i,3)/Ab(3,3);
L(i,3) = alpha;
Ab(i,:) = Ab(i,:) - alpha*Ab(3,:);
U = Ab(1:n,1:n);

output=============

L=

1.0000 0 0 0

0.2500 1.0000 0 0

0.8750 0.1364 1.0000 0

1.1250 1.6818 -4.2464 1.0000

incredible1 Page 3
4

>> U =

8.0000 -2.0000 3.0000 4.0000……………………………………………………………………(3)

0 5.5000 -7.7500 0

0 0 -1.5682 -1.5000

0 0 0 -8.8696

incredible1 Page 4

You might also like