Computers in Engineering Tutorial 1
Name of Lecturer: Mr. R. Senior
Name of Laboratory Technologist: Mr. V. Alexander
Student name: Landel Smith
Id number: 1607757
Tutorial Script
(1+sqrt(5))/2
ans =
1.6180
%The result is stored under the Workspace tab with the variable name 'ans'
GoldenRatio=(1+sqrt(5))/2
GoldenRatio =
1.6180
%Question 2
((2^3)+7)/((3^2)-sqrt(16))
ans =
%Manual checks match with MATLAB output
%Question 3
x=0;
a=cos(x)
a=
Page | 1
1
b=sin(x)
b=
c=tan(x)
c=
x=pi/2
x=
1.5708
a=cos(x)
a=
6.1232e-17
b=sin(x)
b=
Page | 2
1
c=tan(x)
c=
1.6331e+16
x=pi/4;
a=cos(x)
a=
0.7071
b=sin(x)
b=
0.7071
c=tan(x)
c=
1.0000
x=pi/6;
Page | 3
a=cos(x)
a=
0.8660
b=sin(x)
b=
0.5000
c=tan(x)
c=
0.5774
x=pi;
a=cos(x)
a=
-1
b=sin(x)
b=
Page | 4
1.2246e-16
c=tan(x)
c=
-1.2246e-16
x=2*pi;
a=cos(x)
a=
b=sin(x)
b=
-2.4493e-16
c=tan(x)
c=
-2.4493e-16
[0 (pi/2) (pi/4) (pi/6) pi (pi*2)]
Page | 5
ans =
0 1.5708 0.7854 0.5236 3.1416 6.2832
x=[0 (pi/2) (pi/4) (pi/6) pi (pi*2)]
x=
0 1.5708 0.7854 0.5236 3.1416 6.2832
a=cos(x)
a=
1.0000 0.0000 0.7071 0.8660 -1.0000 1.0000
b=sin(x)
b=
0 1.0000 0.7071 0.5000 0.0000 -0.0000
c=tan(x)
c=
1.0e+16 *
0 1.6331 0.0000 0.0000 -0.0000 -0.0000
Page | 6
%Question 4
r=1:4;
area=pi*(r^2)
{Error using <a href="matlab:matlab.internal.language.introspective.errorDocCallback('mpower')"
style="font-weight:bold"> ^ </a>
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a
scalar. To
perform elementwise matrix powers, use '.^'.
}
area=pi*(r.^2)
area =
3.1416 12.5664 28.2743 50.2655
a=0:2:10
a=
0 2 4 6 8 10
help linespace
--- <strong>linespace</strong> not found. Showing help for <strong>linspace</strong> instead. ---
<strong>linspace</strong> Linearly spaced vector.
<strong>linspace</strong>(X1, X2) generates a row vector of 100 linearly
equally spaced points between X1 and X2.
Page | 7
<strong>linspace</strong>(X1, X2, N) generates N points between X1 and X2.
For N = 1, <strong>linspace</strong> returns X2.
Class support for inputs X1,X2:
float: double, single
See also <a href="matlab:help logspace">logspace</a>, <a href="matlab:help colon">colon</a>.
<a href="matlab:doc linspace">Reference page for linspace</a>
<a
href="matlab:matlab.internal.language.introspective.overloads.displayOverloads('linspace')">Other
functions named linspace</a>
linspace(0,10,2)
ans =
0 10
linspace(0,10,6)
ans =
0 2 4 6 8 10
b=linspace(0,10,6)
b=
Page | 8
0 2 4 6 8 10
c=a+b
c=
0 4 8 12 16 20
%The size of c is 1X6
%Question #6
X=0:3:2pi
X=0:3:2pi
{Error: Invalid expression. Check for missing multiplication operator, missing or unbalanced delimiters,
or other syntax
error. To construct matrices, use brackets instead of parentheses.
}
X = 0:3:2*pi
X=
0 3 6
X=0:3:(2*pi)
X=
0 3 6
Page | 9
Ya=cos(X)
Ya =
1.0000 -0.9900 0.9602
Yb=sin(X)
Yb =
0 0.1411 -0.2794
plot(X, Ya, X, Yb)
[Warning: MATLAB has disabled some advanced graphics rendering features by switching to software
OpenGL. For more
information, click <a href="matlab:opengl('problems')">here</a>.]
Page | 10
%Question 7
x=0:0.25:8;
y=(3*x)+2;
plot(x,y)
x=0:0.1:2;
y=exp(-x^2);
{Error using <a href="matlab:matlab.internal.language.introspective.errorDocCallback('mpower')"
style="font-weight:bold"> ^ </a>
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a
scalar. To
perform elementwise matrix powers, use '.^'.
}
y=exp(-x.^2);
plot(x,y)
Page | 11
x=0:pi/10:2*pi
x=
Columns 1 through 12
0 0.3142 0.6283 0.9425 1.2566 1.5708 1.8850 2.1991 2.5133 2.8274 3.1416
3.4558
Columns 13 through 21
3.7699 4.0841 4.3982 4.7124 5.0265 5.3407 5.6549 5.9690 6.2832
y=cos(x);
plot(x,y)
Page | 12
x=1:0.5:4;
y=(ln(exp(x)))^-1
{Undefined function or variable 'ln'.
}
help ln
ln not found.
Use the Help browser search field to <a href="matlab:docsearch ln">search the documentation</a>, or
type "<a href="matlab:help help">help help</a>" for help command options, such as help for methods.
look ln
{Undefined function or variable 'look'.
}
logo ln
{Attempt to execute SCRIPT logo as a function:
Page | 13
C:\Program Files\MATLAB\R2018b\toolbox\matlab\general\logo.m
}
where ln
{Undefined function or variable 'where'.
}
iskeyword ln
ans =
<a href="matlab:helpPopup logical" style="font-weight:bold">logical</a>
which ln
'ln' not found.
y=(log(exp(x)))^-1;
{Error using <a href="matlab:matlab.internal.language.introspective.errorDocCallback('mpower')"
style="font-weight:bold"> ^ </a>
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a
scalar. To
perform elementwise matrix powers, use '.^'.
}
y=(log(exp(x))).^-1;
plot(x,y)
Page | 14
%Question #8
A=[1 2 3 4; 11 12 13 14; 21 22 23 24]
A=
1 2 3 4
11 12 13 14
21 22 23 24
size A
ans =
1 1
size(A)
Page | 15
ans =
3 4
VecA=A(3,:)
VecA =
21 22 23 24
VecB=A(:,2)
VecB =
12
22
Ele=A(3,4)
Ele =
24
%Element in the 3rd row 4th column is 24
A'
ans =
Page | 16
1 11 21
2 12 22
3 13 23
4 14 24
A[1 2; 34];
A[1 2; 34];
{Error: Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise,
check for
mismatched delimiters.
}
A=[1 2; 3 4];
B=[2 2; 2 2];
%Question 9
A=[1 2; 3 4];
B=[2 2; 2 2];
A+B
ans =
3 4
5 6
A-B
ans =
Page | 17
-1 0
1 2
A*B
ans =
6 6
14 14
A.*B
ans =
2 4
6 8
A^2
ans =
7 10
15 22
A*A
ans =
7 10
Page | 18
15 22
A.^2
ans =
1 4
9 16
A^B
{Error using <a href="matlab:matlab.internal.language.introspective.errorDocCallback('mpower')"
style="font-weight:bold"> ^ </a>
Incorrect dimensions for raising a matrix to a power. Check that the matrix is square and the power is a
scalar. To
perform elementwise matrix powers, use '.^'.
}
A.^B
ans =
1 4
9 16
A=[1 2 3; 4 5 6];
size(A)
ans =
2 3
Page | 19
reshape(A,1,6)
ans =
1 4 2 5 3 6
Av=reshape(A,1,6)
Av =
1 4 2 5 3 6
min(Av)
ans =
max(Av)
ans =
Page | 20