Day 8:
We started the day with introduction into for loops. And then moved onto while loops. And finally continued onto nested loops.
Here is a picture for the lack of pictures. Shows the badness of for and while loops in Matlab.
Conclusion:
While loops are bad. Do not do them. Nested loops can get you in trouble use caution when decided to use them. For loops can be useful.
HMWK 8
%% Homework 8
%8-1
g = [3.7 8.87 9.81 1.6 3.7 23.12 8.96 8.69 11.0 0.58] %gravity constants
t = [0:100] %times
[g1,t1] = meshgrid(g,t) %meshgrid of two matrices
d = .5.*g1.*t1.^2 %distance of times 0 t0 100
dmax = max(d) %max distane travled per gravity
fprintf('distance in meters for: mercury %4.2f, venus %4.2f earth %4.2f moon %4.2f mars %4.2f jupiter %4.2f saturn %4.2f uranus %4.2f neptune %4.2f pluto %4.2f', dmax)
%8-2
a = [15, 3, 22; 3, 8, 5; 14, 3, 82]
b = [1; 5; 6]
c = [12 18 5 2]
d = a(:,3)
e = [b,d]
f = [d b]
g = c(:,1)
h = [a(1,3),c(1,2),b(2,1)]
%8-3
a = magic(5) %magic 5 matrix
b = a.*2 %multiplying matrix by 2
d = a.^2
sum(b) %sum of b1 column same,yes
sum(b')' %sum of b1 row same, yes
sum(diag(b)) %sum of b1 diagonal, yes
sum(fliplr(diag(b))) %sum of flipped diagnoal, yes
sum(d) %sum of b1 column same,yes
sum(d')' %sum of b1 row same, yes
sum(diag(d)) %sum of b1 diagonal, yes
sum(fliplr(diag(d))) %sum of flipped diagnoal, yes
c = a+2 % adding 2 to matrix a
sum(c) %sum of c1 column same, yes
sum(c')' %sum of c1 row same, yes
sum(diag(c)) %sum of c1 diagonal,yes
sum(fliplr(diag(c))) %sum of flipped diagnoal, yes
e = [a,a;a,a] %10 by 10
f = e.*2 %magic 10 times 2
g = e.^2 %magic 10 power 2
h = e+2 %magic 10 plus 2
No comments:
Post a Comment