Professional Documents
Culture Documents
Assignment For Module-1: Instructions
Assignment For Module-1: Instructions
INSTRUCTIONS
1. DEMO PROBLEM
2. VECTOR SUMMATION
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
Columns 8 through 11
-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);
INSTRUCTIONS
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
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
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.
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
output
coshVal = maclaurinCosh(1.5,4)
eve1 = 0 1.1250 0 0.2109
coshVal = 2.3359
>> coshVal = maclaurinCosh(3,9)
coshVal = 10.0502
Problem no-3
output
nonconvergent(500)
s = 6.7928
nonconvergent(1000)
s = 7.4855
nonconvergent(5000)
s = 9.0945
nonconvergent(10000)
s = 9.7876
Problem no-4
output
multistepTaylor(5)
y = 0.4226
multistepTaylor(40)
y = 1.6502
MATLAB Programming for Numerical Computations Jan–March 2017
INSTRUCTIONS
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
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
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
page 1:Incredible1
output (n=5)
multistepintegral
For h=0.2,Error=0.027118
0.7454…………………………………………….(1)
For h=0.2,Error=0.027118
0.7454
0.0271
output(n=25)
>> multistepintegral
0.7194………………………………………………………………..(2)
For h=0.04,Error=0.0010872
0.7194
For h=0.04,Error=0.0010872
0.7194
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)
0.7186
For h=0.02,Error=0.00027182
0.7186
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;
Q = quad(@(x) hw3fun(x,1),0,1)
err=abs(Q-trueval)
output
Q = 0.7183………….(5)
err = 4.1583e-09…………………(6)
Q = 1.5973…………………………….(7)
page 3:Incredible1
Example3
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;
I_simp=h/3*(fvec(1)+4*sum(fvec(1:2:n))+2*sum(fvec(2:2:n))+fvec(n+1));
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
page 5:Incredible1
MATLAB Programming for Numerical Computations Jan–March 2017
INSTRUCTIONS
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.
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
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
%% 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
incredible1 Page 3
4
>> U =
0 5.5000 -7.7500 0
0 0 -1.5682 -1.5000
0 0 0 -8.8696
incredible1 Page 4