1. Introduction
>> 3*4+6
ans =
18
5
is in radians.
>> cos (5)
ans =
0.2837
>> x = 4
x =
4
>> 3*sqrt(6+x)
ans =
9.4868
>> x = 3;
>> y = 4;
>> z = 2*x - 7*y
z =
-22
>> help inv
INV Matrix inverse.
INV(X) is the inverse of the square matrix X.
A warning message is printed if X is badly scaled or nearly singular.
See also slash, pinv, cond, condest, lsqnonneg, lscov.
Overloaded functions or methods (ones with the same name in other directories) help sym/inv.m
Reference page in Help browser doc inv
>> A = [1 0 2 -3 ; 0 5 2 2 ; 1 2 3 4 ; -2 0 1 3]
A =
1 0 2 -3
0 5 2 2
1 2 3 4
-2 0 1 3
>> b = [1 ; 2 ; 3 ; 4]
b =
1
2
3
4
>> A = [ 5 2 ; 1 3]
A =
5 2
1 3
>> b = [3 ; −1]
b =
3
-1
>> x = A\b
x =
0.8462
-0.6154
>> X = [1 0 6 ; 1 2 3 ; 4 5 -2]
X =
1 0 6
1 2 3
4 5 -2
>> X(2:3,1:2)
ans =
1 2
4 5
>> X(1:3,2)
ans =
0
2
5
>> X(1,1:3)
ans =
1 0 6
>> X(1,3)
ans =
6
>> x = [1 2 3 4 5 6 7 8 9]
x =
1 2 3 4 5 6 7 8 9
>> y = x.^3 - 2
y =
-1 6 25 62 123 214 341 510 727
>> plot(x,y)
>> magic(4)
ans =
16 2 3 13
5 11 10 8
9 7 6 12
4 14 15 1
The sum of each row, column, or diagonal is 34.
2. Arithmetic Operations
>> 16−10
ans =
6
>> 2*9
ans =
18
>> 12/3
ans =
4
>> 12/5
ans =
2.4000
>> 3^(-5)
ans =
0.0041
>> (−3)^5
ans =
−243
>> −3^5
ans =
−243
>> 2*pi/3
ans =
2.0944
>> 5+7-15
ans =
-3
>> 6*7 +4
ans =
46
>> 6*(7+4)
ans =
66
>> 4.5 + 15/2
ans =
12
>> (15–4+12) /5 – 2*(7^4)/100
ans =
-43.4200
>> (15–4) + 12/5 – (2*7)^4/100
ans =
−370.7600
>> sym(2/3)
ans =
2/3
>> 2/3 + 3/4
ans =
1.4167
3. Variables
w
.
>> 2*3+7
ans =
13
a, b
, and c
equal to 4, −10
, and 3.2
, respectively.
>> a = 4
a =
4
>> b = −10
b =
−10
>> c = 3.2
c =
3.2000
y
and Y
equal to 10
and 100
. Are the two variables identical?
>> y = 10
y =
10
>> Y = 100
Y =
100
The two variables are not identical.
x
= 5.5
and y
= −2.6
. Calculate the value of the variable z = 2x−3y
.
x =
5.5000
>> y = −2.6
y =
−2.6000
>> z = 2*x − 3*y
z =
18.8000
w
= 3y
− z + x/y
.
r
= 6.3
and s = 5.8
. Calculate the value of the variable final
defined by final
= r + s - r*s
.
>> r = 6.3
r =
6.3000
>> s = 5.8
s =
5.8000
>> final = r + s - r*s
final =
-24.4400
this_is_the_result
defined by this_is_the_result = r^2 – s^2.
>> this_is_the_result = r^2 − s^2
this_is_the_result =
6.0500
width, Width
, and WIDTH
equal to 1.5, 2.0, and 4.5, respectively. Are these three variables identical?
The three variables are not identical.
This line will not be executed
.
% This line will not be executed.
3.5
to the variable s
then add a comment about this assignment on the same line.
>> s = 3.5 % the variable s is assigned the value 3.5
s =
3.5000
y1
and y2
equal to 7
and 9
then perform the calculation y3 = y1 – y2/3
. (Note: 2 in the formula is a subscript and should not be divided by 3).
2*m – 5
. Do you get an error? Why?
>> 2*m − 5
??? Undefined function or variable 'm'.
We get an error message because the variable m
is not defined.
cost
and profit
equal to 175
and 25
, respectively, then calculate the variable sale_price
defined by sale_price
= cost + profit
.
centigrade
equal to 28
then calculate the variable fahrenheit
defined by fahrenheit = (centigrade*9/5) + 32
.
>> centigrade = 28
centigrade =
28
>> fahrenheit = (centigrade*9/5) + 32
fahrenheit =
82.4000
format short
and format long
commands to write the values of 14/9
to four decimals and sixteen digits, respectively.
>> format long
>> 14/9
ans =
1.55555555555556
>> format short
>> 14/9
ans =
1.5556
who
command to get a list of the variables stored in this session.>> who
Your variables are:
WIDTH fahrenheit x
Width final y
Y profit y1
a r y2
ans s y3
b sale_price z
c this_is_the_result centigrade
w cost width
whos
command to get a list of the variables stored in this session along with their details.>> whos
clear
command.
>> clear
>>
a =
5
>> b = 7
b =
7
>> area = a*b
area =
35
>> perimeter = 2*(a+b)
perimeter =
24
>> r = 6.45
r =
6.4500
>> area = pi*r^2
area =
130.6981
>> perimeter = 2*pi*r
perimeter =
40.5265
>> y = sym(2*x − z)
y =
66/85
>> radius1 = 2/3
radius1 =
0.6667
>> radius1 = sym(radius1)
radius1 =
2/3
>> area = pi*radius1^2
area =
4/9*pi
>> radius2 = 2/3
radius2 =
0.6667
>> radius2 = sym(radius2)
radius2 =
2/3
>> volume = (4/3)*pi*radius2^3
volume =
32/81*pi
double
command to obtain the numerical value of the answer.
>> double(area)
ans =
1.3963
double
command to obtain the numerical value of the answer.
>> double(volume)
ans =
1.2411
y
and date
without assigning any numerical values to them.
>> y = sym('y')
y =
y
>> date = sym('date')
date =
date
4. Mathematical Functions
>> sqrt(10)
ans =
3.1623
>> factorial(7)
ans =
5040
>> cos(45*pi/180)
ans =
0.7071
>> sin(45*pi/180)
ans =
0.7071
>> tan(45*pi/180)
ans =
1.0000
>> atan(1.5)
ans =
0.9828
The above result is in radians.
We get a very large number approaching infinity because the tanget function is not define at .
>> exp(3)
ans =
20.0855
>> log(3.5)
ans =
1.2528
>> log10(3.5)
ans =
0.5441
round
to round the value of 2.43.
rem
to obtain the remainder when dividing 5 by 4.
>> rem(5,4)
ans =
1
>> abs(-3.6)
ans =
3.6000
>> 1.5 - 2*sqrt(6.7/5)
ans =
-0.8152
>> sin(pi)^2 + cos(pi)^2
ans =
1
We get minus infinity because the logarithmic function is not defined at zero.
>> x = 3*pi/2
x =
4.7124
>> y = 2*pi
y =
6.2832
>> 2*sin(x)*cos(y)
ans =
-2
>> double(ans)
ans =
6.7082
>> sym(sin(45*pi/180))
ans =
sqrt(1/2)
> sym(cos(45*pi/180))
ans =
sqrt(1/2)
>> sym(exp(pi/2))
ans =
5416116035097439*2^(-50)
>> double(ans)
ans =
4.8105
5. Complex Numbers
>> sqrt(-5)
ans =
0 + 2.2361i
>> 4-3*sqrt(-8)
ans =
4.0000 - 8.4853i
>> x+y
ans =
6.0000 + 5.0000i
>> x-y
ans =
-2.0000 -17.0000i
>> x^4
ans =
4.4800e+002 +1.5360e+003i
>> y^(-3)
ans =
-5.3979e-004 +3.1229e-004i
>> 4*x-3*y+9
ans =
5.0000 -57.0000i
>> i*x-2*y-1
ans =
-3.0000 -20.0000i
>> angle(3−5i)
ans =
-1.0304
>> angle(3-5i)*180/pi
ans =
-59.0362
>> real(3−5i)
ans =
3
>> imag(3−5i)
ans =
−5
>> sin(3-5i)
ans =
10.4725 +73.4606i
>> cos(3-5i)
ans =
-73.4673 +10.4716i
>> tan(3-5i)
ans =
-0.0000 - 0.9999i
>> exp(3-5i)
ans =
5.6975 +19.2605i
>> log(3-5i)
ans =
1.7632 - 1.0304i
>> sin(pi*i/2)
ans =
0 + 2.3013i
>> cos(pi*i/2)
ans =
2.5092
>> exp(pi*i/2)
ans =
0.0000 + 1.0000i
>> (3+4i)^(2-i)
ans =
61.3022 +15.3369i
>> sym(sqrt(-13))
ans =
(0)+(sqrt(13))*i
double
command at the end.
>> sym(angle(3-5i))
ans =
-4640404691986088*2^(-52)
>> double(ans)
ans =
-1.0304
double
command at the end.
>> sym(cos(3-5i))
ans =
(-5169801091137321*2^(-46))+(5894962905280379*2^(-49))*i
>> double(ans)
ans =
-73.4673 +10.4716i
6. Vectors
>> w = [2 4 -6 0] w
=
2 4 -6 0
> w(2)
ans =
4
>> z = pi*w/2
z =
3.1416 6.2832 -9.4248 0
>> z(4)
ans =
0
>> length(z)
ans =
4
>> sum(z)
ans =
0
>> min(z)
ans =
-9.4248
>> max(z)
ans =
6.2832
>> s = linspace(1,100,10)
s =
1 12 23 34 45 56 67 78 89 100
>> a = [9 3 -2 5 0]
a =
9 3 -2 5 0
>> b = [1 2 -4]
b =
1 2 -4
>> c = [a b]
c =
9 3 -2 5 0 1 2 -4
>> x = [0.2 1.3 -3.5]
x =
0.2000 1.3000 -3.5000
>> y = [0.5 -2.5 1.0]
y =
0.5000 -2.5000 1.0000
>> x+y
ans =
0.7000 -1.2000 -2.5000
>> x-y
ans =
-0.3000 3.8000 -4.5000
We get an error because the two vectors do not have the same length.
>> x.*y
ans =
0.1000 -3.2500 -3.5000
>> x./y
ans =
0.4000 -0.5200 -3.5000
>> x*y'
ans =
-6.6500
>> r = [ 1 3 5 ]
r =
1 3 5
>> s = [ 3 6 ]
s =
3 6
>> r+s
??? Error using ==> plus
Matrix dimensions must agree.
We get an error message because the two vectors do not have the same length.
>> r-s
??? Error using ==> minus
Matrix dimensions must agree.
We get an error message because the two vectors do not have the same length.
>> w = [0.1 1.3 -2.4]
w =
0.1000 1.3000 -2.4000
>> 5+w
ans =
5.1000 6.3000 2.6000
>> 1.5*w
ans =
0.1500 1.9500 -3.6000
>> w/10
ans =
0.0100 0.1300 -0.2400
>> 3-2*w/5
ans =
2.9600 2.4800 3.9600
>> exp(b)
ans =
1.0000 2.8497 8.1205 23.1407
>> sqrt(b)
ans =
0 1.0233 1.4472 1.7725
Yes, because this operation needs to be performed element by element.
>> 3.^b
ans =
1.0000 3.1597 9.9834 31.5443
> ones(1,4)
ans =
1 1 1 1
>> zeros(1,6)
ans =
0 0 0 0 0 0
>> randperm(5)
ans =
2 4 3 5 1
>> syms r s t u v
>> x = [r s t u v] x
= [ r, s, t, u, v]
x
and [1 0 -2 3 5] to obtain the new symbolic vector y
.
>> r = [1 0 -2 3 5]
r =
1 0 -2 3 5
>> y = x+r
y =
[ r+1, s, t-2, u+3, v+5]
y
.
>> y(3)
ans =
t-2
2*x/7 + 3*y
.
>> 2*x/7 + 3*y
ans =
[ 23/7*r+3, 23/7*s, 23/7*t-6, 23/7*u+9, 23/7*v+15]
>> x*y'
ans =
r*(1+conj(r))+s*conj(s)+t*(-2+conj(t))+u*(3+conj(u))+v*(5+conj(v))
>> y*x'
ans =
(r+1)*conj(r)+s*conj(s)+(t-2)*conj(t)+(u+3)*conj(u)+(v+5)*conj(v)
The two results are different because MATLAB treats these symbolic variables as complex variables by default. If they were real variables, then the results would be the same.
x+y
.
>> sqrt(x+y)
ans =
[ (2*r+1)^(1/2), 2^(1/2)*s^(1/2), (2*t-2)^(1/2), (2*u+3)^(1/2), (2*v+5)^(1/2)]
7. Matrices
>> A = [3 0 -2 ; 1 3 5]
A =
3 0 -2
1 3 5
A is a rectangular matrix of size 2x3.
>> A(2,2)
ans =
3
>> B = 3*pi*A/2
B =
14.1372 0 -9.4248
4.7124 14.1372 23.5619
>> B(1:2,2:3)
ans =
0 -9.4248
14.1372 23.5619
The above sub-matrix has size 2x2.
>> size(B)
ans =
2 3
>> length(B)
ans =
3
>> sum(B)
ans =
18.8496 14.1372 14.1372
>> min(B)
ans =
4.7124 0 -9.4248
>> max(B)
ans =
14.1372 14.1372 23.5619
>> R.*S
ans =
1 6 0
21 25 -21
6 3 0
>> R./S
Warning: Divide by zero.
ans =
1.0000 0.6667 0
2.3333 1.0000 -0.4286
1.5000 0.3333 Inf
Notice the division by zero (not allowed) above in the last element.
>> R*S
ans =
7 13 12
16 37 21
8 17 1
We do not get an error because the number of columns of the first matrix is equal to the number of rows of the second matrix.
>> X-3
ans =
-2 -5 -3 -2
-1 0 3 -1
-6 2 -1 -2
2 -5 1 1
>> -3*X
ans =
-3 6 0 -3
-6 -9 -18 -6
9 -15 -6 -3
-15 6 -12 -12
>> -3*X/2.4 + 5.5
ans =
4.2500 8.0000 5.5000 4.2500
3.0000 1.7500 -2.0000 3.0000
9.2500 -0.7500 3.0000 4.2500
-0.7500 8.0000 0.5000 0.5000
>> sqrt(B)
ans =
1.0233 1.4472
1.4472 1.7725
>> sqrtm(B)
ans =
0.5821 + 0.3598i 0.9419 - 0.2224i
0.9419 - 0.2224i 1.5240 + 0.1374i
Notice that we obtained a complex matrix. Try to check this answer by multiplying this complex matrix by itself to get the original matrix.
>> expm(B)
ans =
23.9028 37.4119
37.4119 61.3147
>> log(B)
ans =
0.0461 0.7393
0.7393 1.1447
>> logm(B)
Warning: Principal matrix logarithm is not defined for A with
nonpositive real eigenvalues. A non-principal matrix
logarithm is returned.
> In funm at 153
In logm at 27
ans =
-0.5995 + 2.2733i 1.2912 - 1.4050i
1.2912 - 1.4050i 0.6917 + 0.8683i
Notice that we obtain a non-principal complex matrix.
>> 4^B
ans =
130.0124 209.2159
209.2159 339.2283
>> 4.^B
ans =
4.2705 18.2369
18.2369 77.8802
>> B^4
ans =
107.0297 173.1717
173.1717 280.2015
>> zeros(2,3)
ans =
0 0 0
0 0 0
>> eye(2,3)
ans =
1 0 0
0 1 0
>> ones(4)
ans =
1 1 1 1
1 1 1 1
1 1 1 1
1 1 1 1
>> eye(4)
ans =
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
>> C = [1 2 -3 0 ; 2 5 2 -3 ; 1 3 7 -2 ; 2 3 -1 3]
C =
1 2 -3 0
2 5 2 -3
1 3 7 -2
2 3 -1 3
>> C'
ans =
1 2 1 2
2 5 3 3
-3 2 7 -1
0 -3 -2 3
>> C + C'
ans =
2 4 -2 2
4 10 5 0
-2 5 14 -3
2 0 -3 6
We get a symmetric matrix.
>> diag(C)
ans =
1
5
7
3
>> det(C)
ans =
-13
>> trace(C)
ans =
16
Both the determinant and trace are scalars (numbers in this example).
>> inv(C)
ans =
-10.5385 6.3846 -6.0000 2.3846
5.3077 -3.0769 3.0000 -1.0769
-0.3077 0.0769 -0.0000 0.0769
1.6154 -1.1538 1.0000 -0.1538
>> C*inv(C)
ans =
1.0000 0.0000 0.0000 0.0000
-0.0000 1.0000 0 0.0000
0 0.0000 1.0000 0
0.0000 0.0000 0 1.0000
We get the identity matrix.
>> norm(C)
ans =
9.5800
>> eig(C)
ans =
-0.0702
3.9379 + 2.6615i
3.9379 - 2.6615i
8.1944
We get four eigenvalues – some of them are complex numbers.
>> poly(C)
ans =
1.0000 -16.0000 86.0000 -179.0000
-13.0000
We get a fourth-degree polynomial with five coefficients.
>> rank(C)
ans =
4
>> rand(5)
ans =
0.9501 0.7621 0.6154 0.4057 0.0579
0.2311 0.4565 0.7919 0.9355 0.3529
0.6068 0.0185 0.9218 0.9169 0.8132
0.4860 0.8214 0.7382 0.4103 0.0099
0.8913 0.4447 0.1763 0.8936 0.1389
>> magic(7)
ans =
30 39 48 1 10 19 28
38 47 7 9 18 27 29
46 6 8 17 26 35 37
5 14 16 25 34 36 45
13 15 24 33 42 44 4
21 23 32 41 43 3 12
22 31 40 49 2 11 20
The sum of each row, column or diagonal is 175.
>> syms x
>> X = [1 x ; x-2 x^2]
X =
[ 1, x]
[ x-2, x^2]
>> Y = [x/2 3*x ; 1-x 4]
Y =
[ 1/2*x, 3*x]
[ 1-x, 4]
>> Z = X-Y
Z =
[ 1-1/2*x, -2*x]
[ 2*x-3, x^2-4]
In the above, it is assumed that x is a complex variable.
>> trace(Z)
ans =
-3-1/2*x+x^2
>> det(Z)
ans =
5*x^2-4-1/2*x^3-4*x
>> inv(Z)
ans =
[ -2*(x^2-4)/(-10*x^2+8+x^3+8*x),
-4*x/(-10*x^2+8+x^3+8*x)]
[ 2*(2*x-3)/(-10*x^2+8+x^3+8*x),
(x-2)/(-10*x^2+8+x^3+8*x)]
The inverse of Z obtained above may be simplified by factoring out the determinant that appears common in the denominators of all the elements of the matrix.
8. Programming
cost = 200
and sale_price = 250
, respectively. The fourth line should have the calculation profit = sale_price – cost
. Store the script in a script file called example8
.m. Finally run the script file.
% This is an example
cost = 200
sale_price = 250
profit = sale_price – cost
Now, run the above script as follows:
>> example8
cost =
200
sale_price =
250
profit =
50
r
. The first line should include the name of the function which is volume(r)
. The second line should be a comment line. The third line should include the calculation of the volume of the sphere which is volume.m
then run the function with the value of r
equal to 2 (no units are used in this exercise).
function volume(r)
% This is a function
volume = (4/3)*pi*r^3
Now, run the above function as follows:
>> volume(2)
volume =
33.5103
a
and b
. The function should have three lines. The first line should include the name of the function which is RectangleArea(a,b)
. The second line should be a comment line. The third line should include the calculation of the area of the rectangle with is the product a*b
. Store the function in a function file called RectangleArea.m
then run the function twice as follow: the first execution with the values 3
and 6
, while the second execution with the values 2.5
and 5.5.
function RectangleArea(a,b)
% This is another example of a function
RectangleArea = a*b
Now, run the above function as follows:
For loop
to compute the vector x
to have the values x(n) = n3 where n
has the range from 1
to 7
. Include a comment line at the beginning. Store the script in a script file called example9.m
then run the script and display the values of the elements of the vector x
.
% This is an example of a FOR loop
for n = 1:7
x(n) = n^3;
end
Now, run the above script as follows:
>> example9
>> x
x =
1 8 27 64 125 216 343
For
loops to compute the matrix y to have the values y(m,n) = m2-n2 where both m
and n
each has the range from 1
to 4
. Include a comment line at the beginning. Store the script in a script file called example10.m
then run the script and display the values of the elements of the matrix y
.
%This is an example of another FOR loop
for n = 1:4
for m = 1:4
y(m,n) = m^2 - n^2;
end
end
Now, run the above script as follows:
While loop
using the two variables tol
and n
. Before entering the While loop
, initialize the two variables using the assignments tol = 0.0
and n = 3
. Then use the two computations n = n + 1
and tol = tol + 0.1
inside the While loop
. Make the loop end when the value of tol
becomes equal or larger than 1.5
. Include a comment line at the beginning. Store the script in a script file called example11.m
then run the script and display the values of the two variables tol
and n
.
% This is an example of a While loop
tol = 0.0;
n = 3;
while tol < 1.5
n = n + 1;
tol = tol + 0.1;
end
Now, run the above script as follows:
price(items)
containing an If
construct as follows. Let the price of the items be determined by the computation price = items*130
unless the value of the variable items
is greater than 5
– then in this case the computation price = items*160
should be used instead. Include a comment line at the beginning. Store the function in a function file called price.m
then run the function twice with the values of 3
and 9
for the variable items
. Make sure that the function displays the results for the variable price
.
function price(items)
% This is an example of an If Elseif construct
price = items*130
if items > 5
price = items*160
end
Now, run the above function as follows:
>> price(3)
price =
390
>> price(9)
price =
1170
price =
1440
price2(items)
containing an If Elseif
construct as follows. If the value of the variable items
is less than 3
, then compute the variable price2
by multiplying items
by 130
. In the second case, if the value of the variable items
is less than 5
, then compute the variable price2
by multiplying items
by 160
. In the last case, if the value of the variable items
is larger than 5
, then compute the variable price2
by multiplying the items
by 200
. Include a comment line at the beginning. Store the function in a function file called price2.m
then run the function three times – with the values of 2, 4,
and 6
. Make sure that the function displays the results for the variable price2
.
function price2(items)
% This is an example of an If Elseif construct
if items < 3
price2 = items*130
elseif items < 5
price2 = items*160
elseif items > 5
price2 = items*200
end
Now, run the above function as follows:
price3(items)
containing a Switch Case
construct. The function should produce the same results obtained in Exercise 8 above. Include a comment line at the beginning. Store the function in a function file called price3.m
then run the function three times – with the values of 2, 4
, and 6
. Make sure that the function displays the results for the variable price3
.
function price3(items)
% This is an example of the Switch Case construct
switch items
case 2
price3 = items* 130
case 4
price3 = items* 160
case 6
price3 = items* 200
otherwise
price3 = 0
end
Now, run the above function as follows:
A
then calculate its third power B = A3. Include a comment line at the beginning. Store the script in a script file called example12.m
then run the script to display the two matrices A
and B
.
% This is an example with the Symbolic
Math Toolbox
syms x
A = [x/2 1-x ; x 3*x]
B = A^3
Now, run the above script as follows:
>> example12
A =
[1/2*x, 1-x]
[ x, 3*x]
B =
[ 1/2*x*(1/4*x^2+(1-x)*x)+7/2*(1-x)*x^2,
7/4*(1-x)*x^2+(1-x)*((1-x)*x+9*x^2)]
[ x*(1/4*x^2+(1-x)*x)+21/2*x^3,
7/2*(1-x)*x^2+3*x*((1-x)*x+9*x^2)]
SquareRoot2(matrix)
similar to the function SquareRoot(matrix)
described at the end of this chapter but with the following change. Substitute the value of 1.5
instead of 1
for the symbolic variable x
. Make sure that you include a comment line at the beginning. Store the function in a function file called SquareRoot2.m
then run the function using the following symbolic matrix:
function SquareRoot2(matrix)
% This is an example of a function
with the Symbolic Math Toolbox
y = det(matrix)
z = subs(y,1.5)
if z < 1
M = 2*sqrt(matrix)
else
M = sqrt(matrix)
end
Now, run the above function as follows:
9. Graphs
Solve all the exercises using MATLAB. All the needed MATLAB commands for these exercises were presented in this chapter.
>> x = [1 2 3 4 5 6 7]
x =
1 2 3 4 5 6 7
>> y = [10 15 23 43 30 10 12]
y =
10 15 23 43 30 10 12
>> plot(x,y)
>> hold on;
>> title('This is an exercise')
>> xlabel('x-axis')
>> ylabel('y-axis')
x
from -6 to +6. Include a title for the graph as well as labels for the two axes.
>> x = [-6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6];
>> y = 2*x.^3 +5;
>> plot(x,y)
>> hold on;
>> title('This is another exercise')
>> xlabel('values of x')
>> ylabel('values of y')
plot(x,y,'b--o')
x
between 0 and 3π with increments of >> x = 0:pi/4:3*pi;
>> y = 2*sin(x/3);
>> z = 2*cos(x/3);
>> plot(x,y,'--',x,z,':')
>> hold on;
>> grid on;
>> title('This is an exercise in plotting two curves on the same diagram')
>> xlabel('x')
>> ylabel('2sin(x/3) and 2cos(x/3)')
>> legend('2sin(x/3)','2cos(x/3)')
subplot
command. The functions are >> x = [1 2 3 4 5 6 7 8 9 10];
>> y = 2*x.^3 -4;
>> z = x+1;
>> w = 2 - sqrt(x);
>> v = x.^2 +3;
>> subplot(2,2,1);
>> hold on;
>> plot(x,y);
>> subplot(2,2,2);
>> plot(x,z);
>> subplot(2,2,3);
>> plot(x,w);
>> subplot(2,2,4);
>> plot(x,v);
plot3
command to show a three-dimensional curve of the equation z = 2sin(xy). Use the vector x = [1 2 3 4 5 6 7 8 9 10] for both x
and y
. No need to show title or axis information.
>> x = [1 2 3 4 5 6 7 8 9 10];
>> y = [1 2 3 4 5 6 7 8 9 10];
>> z = 2*sin(x.*y);
>> plot3(x,y,z)
mesh
command to plot a three-dimensional mesh surface of elements of the matrix M
given below. No need to show title or axis information.
>> M = [0.1 0.2 0.5 0.7 0.3 ; 0.2 0.8 0.5 0.6 0.3 ; 0.5 0.9 0.9 0.4 0.4 ; 0.4 0.4 0.5 0.7 0.9 ; 0.1 0.3 0.4 0.6 0.8]
M =
0.1000 0.2000 0.5000 0.7000 0.3000
0.2000 0.8000 0.5000 0.6000 0.3000
0.5000 0.9000 0.9000 0.4000 0.4000
0.4000 0.4000 0.5000 0.7000 0.9000
0.1000 0.3000 0.4000 0.6000 0.8000
>> mesh(M)
surf
command to plot a three-dimensional surface of the elements of the matrix M
given in Exercise 8 above. No need to show title or axis information.
>> surf(M)
contour
command to plot a contour map of the elements of the matrix M
given in Exercise 8 above. No need to show the contour values on the graph.
surfc
command to plot a three-dimensional surface along with contours underneath it of the matrix M
given in Exercise 8 above. No need to show title or axis information.
>> surfc(M)
3x + 5 = 0
>> p = [3 5]
p =
3 5
>> roots(p)
ans =
-1.6667
x2 + x +1 = 0
>> p = [1 1 1]
p =
1 1 1
>> roots(p)
ans =
-0.5000 + 0.8660i
-0.5000 - 0.8660i
3x4 - 2x3 + x - 3 = 0
>> p = [3 -2 0 1 -3]
p =
3 -2 0 1 -3
>> roots(p)
ans =
1.1170
0.2426 + 0.9477i
0.2426 - 0.9477i
-0.9355
3x + 5y = 9
4x - 7y = 13
>> A = [3 5 ; 4 -7]
A =
3 5
4 -7
>> b = [9 ; 13]
b =
9
13
>> x = A\b
x =
3.1220
-0.0732
2x - y + 3z = 5
4x + 5z = 12
x + y + 2z = -3
2x + a = 5
>> syms x
>> syms a
>> solve('2*x + a - 5 = 0')
ans =
-1/2*a+5/2
x2 + ax + b = 0
>> syms x
>> syms a
>> syms b
>> solve('x^2 + a*x + b = 0')
ans =
-1/2*a+1/2*(a^2-4*b)^(1/2)
-1/2*a-1/2*(a^2-4*b)^(1/2)
2ex+ 3cos x = 0
>> syms x
>> solve('2*exp(x) + 3*cos(x) = 0')
ans =
-1.6936679094546100269188394124169
The above answer is approximated as -1.694.
2x − 3cy =5
cx + 2y =7
3 x2 − 2+ + y = 7
xy + x = 5
>> syms x
>> syms y
>> [x,y] = solve('3*x^2 - 2*x + y - 7 = 0', 'x*y + x - 5 = 0')
x
= 5/3
1/2*5^(1/2)-1/2
-1/2-1/2*5^(1/2)
y =
2
3/2+5/2*5^(1/2)
3/2-5/2*5^(1/2)
11. Beginning Calculus
inline
command:
f (x) = 3x2 + x - 1
f
at x = 1.
>> f(1)
ans =
3
f
at x = –2.
>> f(-2)
ans =
9
f
with respect to x
.
>> syms x
>> diff(f(x),x)
ans =
6*x+1
inline command
:
g(y) = 2sin(πy) + 3y cos(πy)
g
with respect to y
.
>> diff(g(y),y)
ans =
2*cos(pi*y)*pi+3*cos(pi*y)-3*y*sin(pi*y)*pi
g.
>> int(g(y))
ans =
-
2/pi*cos(pi*y)+3/pi^2*(cos(pi*y)+y*sin(pi*y )*pi)
>> int(g(y),0,1)
ans =
2*(2*pi-3)/pi^2
double command
.
>> limit(sin(x)+cos(x),x,0)
ans =
1
>> limit((x^2+x+1)/(3*x^2-2),x,Inf)
ans =
1/3
>> taylor(cos(x),x,8)
ans =
1-1/2*x^2+1/24*x^4-1/720*x^6
>> syms k
>> syms n
>> symsum(1/k,1,n)
ans =
Psi(n+1)+eulergamma
The above result is written in terms of special functions. Consult a book on special function for more details.
dsolve
command:
>> dsolve('Dy = x*y - sin(x) + 3', 'y(0)=0')
ans =
(sin(x)-3+exp(x*t)*(-sin(x)+3))/x