Exercise 1: Evaluate Following Matlab Code
Exercise 1: Evaluate Following Matlab Code
Exercise 1: Evaluate Following Matlab Code
2.6667
b) >> 3*4-5^2*2-3
ans = -41
c) >> 3*(3*4-2*5^2-3)
ans = -123
d) >> (2/3^2*5)*(3-4^3)^2
ans = 4.1344e+03
Exercise 2 :
A) Generate three row vectors V1,V2,V3 and Compute the following .
>>V1=[1 3 sqrt(5)];
>>V2=[7 5];
>>V3=[3 4 5];
a) >> V4=3*V1
V4 =
3.0000
9.0000
6.7082
b) >> V5=2*V1-3*V3
V5 = -7.0000 -6.0000 -10.5279
c) V6=V1+V2
Matrix dimensions must agree.
B) Generate row vector V7 by concatenating existing vectors V3 & V1 using
following expression:
V7=[2*V3,-V1]
V7 =
6.0000
1.1667
2.3333
3.5000
>> V9(1:2:7)
ans =
2.3333
4.6667
7.0000
4.6667
5.8333
7.0000
0.5000
>> c=-2
c=
-2
>> x=[0,1.5,3,4,5,7,9,10]
x=
1.5000
3.0000
4.0000
5.0000
7.0000
9.0000 10.0000
0.5000
1.5000
2.5000
y=m*x+c
y = -2.0000 -1.2500 -0.5000
3.0000
F) t=[1:10]
t=
10
>> x=t.*sin(t)
x = 0.8415 1.8186
3.7091 -5.4402
4.5989
7.9149
>> y=(t-1)./(t+1)
y=
0.8000
0 0.3333
0.8182
0.5000
>> z=(sin(t.^2))./(t.^2)
0.6000
0.6667
0.7143
0.7500
0.7778
z = 0.8415 -0.1892
-0.0078 -0.0051
Exercise 3:
1. 1) c=[1.1 -3.2 3.4 0.6; 0.6 1.2 -0.6 3.1; 1.3 0.6 5.5 0.0]
c=
1.1000 -3.2000
3.4000
0.6000
0.6000
1.2000 -0.6000
3.1000
1.3000
0.6000
5.5000
c(2,:)
ans =
0.6000
1.2000 -0.6000
3.1000
>> c(:,end)
ans =
0.6000
3.1000
0
>> c(1:2,2:end)
ans =
-3.2000
3.4000
0.6000
1.2000 -0.6000
3.1000
>> c(6)
ans =
0.6000
>> c(4:end)
ans = -3.2000
3.1000
0
1.2000
0.6000
>> c(1:2,2:4)
ans =
-3.2000
3.4000
0.6000
1.2000 -0.6000
3.1000
3.4000 -0.6000
5.5000
0.6000
0.0144
>> c(3)=c(1)
ans =
1.1000 -3.2000
3.4000
0.6000
0.6000
1.2000 -0.6000
3.1000
1.1000
0.6000
5.5000
3.4000
0.6000
0.6000 -0.6000
3.1000
c(:,2)=[]
c=
1.1000
1.1000
5.5000
>> c(2,2)=0
c=
1.1000
3.4000
0.6000
1.1000
5.5000
0.6000
3.1000
0
>> a=[1 0 ;2 1]
a=
1
>> b=[-1 2 ;0 1]
b=
-1
2
>> d=5
d=
>> a+b
ans =
0
>> a+c
Error using +
Matrix dimensions must agree.
>> a.*b
ans =
-1
>> a*b
ans =
-1
-2
>> a*c
ans =
3
8
>> a+d
ans =
6
>> a.*d
ans =
5
10
0
5
>> a*d
ans =
5
10
>> a./b
ans =
-1
Inf
>> a./a
ans =
1 NaN
1
>> a.^2
ans =
1
>>E=5*eye(3,3)
E=
5
>> F=3*ones(2,2)
F=
3
Example:
subplot(2,1,1), PLOT(income)
subplot(2,1,2), PLOT(outgo)
plots income on the top half of the window and outgo on the
bottom half. If the CurrentAxes is nested in a uipanel the
panel is used as the parent for the subplot instead of the
current figure.
10.xlabel X-axis label.
xlabel('text') adds text beside the X-axis on the current axis.
11.ylabel Y-axis label.
ylabel('text') adds text beside the Y-axis on the current axis.
12.input Prompt for user input.
Example:
reply = input('Do you want more? Y/N [Y]:','s');
if isempty(reply)
reply = 'Y';
end
13.eye Identity matrix.
eye(N) is the N-by-N identity matrix.
Example:
x = eye(2,3,'int8');
14.log10 Common (base 10) logarithm.
log10(X) is the base 10 logarithm of the elements of X.
15.floor Round towards minus infinity.
floor(X) rounds the elements of X to the nearest integers
infinity.
towards minus
towards infinity.
complex elements.
19.abs Absolute value.
abs(X) is the absolute value of the elements of X.
20.log Natural logarithm.
log(X) is the natural logarithm of the elements of X.
21.sqrt Square root.
sqrt(X) is the square root of the elements of X.
Complex results are produced if X is not positive.
22.save Save workspace variables to file.
save(FILENAME) stores all variables from the current workspace in a
MATLAB formatted binary file (MAT-file) called FILENAME.
23.load Load data from MAT-file into workspace.
S = load(FILENAME) loads the variables from a MAT-file into a structure
array, or data from an ASCII file into a double-precision array.
Example:
load('handel.mat', 'y')
% Only variable y
24.legend Display legend.
legend(string1,string2,string3, ...) puts a legend on the current plot
using the specified strings as labels.
Examples:
x = 0:.2:12;
plot(x,besselj(1,x),x,besselj(2,x),x,besselj(3,x));
legend('First','Second','Third','Location','NorthEastOutside')