Skip to content
This repository was archived by the owner on Dec 16, 2021. It is now read-only.

Commit cd2b0e4

Browse files
committed
Add lab 5 (topic 2, exercise 1/lab A) solutions
1 parent 190d2df commit cd2b0e4

File tree

4 files changed

+276
-0
lines changed

4 files changed

+276
-0
lines changed

lab05_t2_e1_labA/lab05.m

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
%% Lab A: For loop/Function/Animation
2+
%% Part 0
3+
% Here is a small explanation of how to define a function in MATLAB
4+
%
5+
%
6+
%
7+
% |function [y1,...,yN] = *myfun*(x1,...,xM)| declares a function named |*myfun
8+
% (it can be whatever except Matlab built-in functions)*| that accepts inputs
9+
% |x1,...,xM| and returns outputs |y1,...,yN|. This declaration statement must
10+
% be the first line of the function and you should put all your functions *at
11+
% the end of the code*. Valid function names begin with an alphabetic character,
12+
% and can contain letters, numbers, or underscores.
13+
%
14+
% Not accepted: 2a, 100!, !kj,...
15+
%
16+
% Define a function named |*average* that accepts an *input values x1 and x2*,
17+
% calculates the average of the values, and returns a *single result*. (*check
18+
% the end of the page for function*)
19+
20+
average(2,5)
21+
%%
22+
% Define another function named *area* that accepts an *input values h and w,*
23+
% calculates the area of a rectangle with those values, and returns a *single
24+
% result*. (*check the end of the page for function*)
25+
26+
area(2,3)
27+
%%
28+
%% Part 1
29+
% Define a function at the end of the code and name it *sinplot* that *get a
30+
% range of x* and *number of divisions* as *three inputs* and plot Sin(x) in that
31+
% range and give y values of Sin(x) as output
32+
%
33+
% Test your function between -pi to pi
34+
35+
% write your code here
36+
% test your function
37+
sinplot(-pi,pi,8)
38+
%% Part 2
39+
% Divide the range of [0,20*pi] to 50 interval
40+
%
41+
% Use your *sinplot function* (defined in example 1) to plot Sin(x) in the
42+
% first and second intervals with *division of 10*
43+
44+
x=linspace(0,20*pi,50) %dividing the range
45+
sinplot(x(1),x(2),10); % first interval
46+
sinplot(x(2),x(3),10); %second interval
47+
%sinplot(x(i),x(i+1),10); %ith interval
48+
% ------------------------
49+
% Use a for loop to plot Sin(x) in the rest of the intervals using *sinplot
50+
% function* with *division of 10*
51+
52+
% perform your work here
53+
for i=1:49
54+
% run sinplot in a suitable range of x vector
55+
sinplot(x(i),x(i+1),10);
56+
end
57+
%%
58+
%
59+
%% Part 3
60+
% Animating Sin (x)
61+
% Remove the previous figure using clf
62+
%
63+
% Try to redo the same for loop but with a pause of 0.25 s at each loop to animate
64+
% drawing the plots
65+
66+
clf
67+
% write your code here
68+
% perform your work here
69+
for i=1:49
70+
% run sinplot in a suitable range of x vector
71+
sinplot(x(i),x(i+1),10)
72+
viscircles([x(i+1),sin(x(i+1))],0.05);
73+
pause(0.25)
74+
end
75+
%%
76+
% Use defined coordinate x and output of your *sinplot function* (y) to put
77+
% a *circle* (radius of 0.05) at end of your *sinplot* at each loop
78+
79+
% viscircles([xcoordinate,ycoordinate],radius);
80+
% ------------------------
81+
% Part 4
82+
% Define a cosplot function similar to sinplot and try to animate both of them
83+
% in the same plot
84+
85+
% perform your work here
86+
clf
87+
88+
for i=1:9
89+
% run sinplot in a suitable range of x vector
90+
sinplot(x(i),x(i+1),10)
91+
cosplot(x(i),x(i+1),10)
92+
pause(0.25)
93+
end
94+
%%
95+
%
96+
%% Functions
97+
% Write your functions here
98+
%
99+
% *Part 0*
100+
101+
function ave = average(x1,x2)
102+
ave = (x1+x2)/2;
103+
end
104+
function y = area(h,w)
105+
y = h*w;
106+
end
107+
%%
108+
% *Part 1*
109+
110+
% perform your work here
111+
% hint: function [y1] = sinplot(x1,x2,division)
112+
% Step 1 : divide the range between x1 to x2 with number of divisions
113+
% define a for loop to replace x values and find the corrosponding y values
114+
115+
function sinx = sinplot(x1,x2,num_divisions)
116+
x = linspace(x1,x2,num_divisions)
117+
sinx = zeros([1, num_divisions]);
118+
for i=1:size(x,2)
119+
sinx(i) = sin(x(i));
120+
end
121+
sinx
122+
plot(x,sinx,"r-");
123+
end
124+
%%
125+
% *Part 4*
126+
127+
% perform your work here
128+
function cosx = cosplot(x1,x2,num_divisions)
129+
x = linspace(x1,x2,num_divisions)
130+
cosx = zeros([1, num_divisions]);
131+
for i=1:size(x,2)
132+
cosx(i) = cos(x(i));
133+
end
134+
cosx
135+
hold on
136+
plot(x,cosx,"b-");
137+
hold off
138+
end

lab05_t2_e1_labA/lab05.mlx

122 KB
Binary file not shown.

lab05_t2_e1_labA/prelab05.m

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
%% Pre-lab A: Loop/Plot/Function
2+
%% [ Your name here ]
3+
%% Exercise 0
4+
%% Keeping track of multiple variables together
5+
% Recall from , we computed the sine and cosine at equally spaced intervals
6+
% and plotted them together.
7+
8+
x = linspace(0, 5*pi, 50)';
9+
y1 = sin(x);
10+
y2 = cos(x);
11+
%%
12+
%
13+
% Everything in MATLAB is a matrix...
14+
% Notice that these are all _vectors._ In MATLAB, everything is a matrix, so
15+
% in this case, these variables are 1-dimensional matrices.
16+
17+
disp('The shape of matrix x is: '), size(x)
18+
disp('The shape of matrix y1 is: '), size(y1)
19+
disp('The shape of matrix y2 is: '), size(y2)
20+
%%
21+
% Clear all defined matrix
22+
23+
clear
24+
%% Exercise 1
25+
% Create an empty matix x
26+
27+
x = []
28+
%%
29+
% Define x between -pi to pi with step size of 0.1
30+
31+
x=-pi:0.1:pi;
32+
33+
%%
34+
% Find the size of x vector
35+
36+
s=size(x) % first column of size shows number of rows and second column shows number of columns
37+
%%
38+
% Create a for loop with *size of x* to show the values of *sin(x)* and index
39+
% of the x
40+
41+
clc
42+
for i=1:s(1,2)
43+
disp('Sinx Value');
44+
sin(x(1,i))
45+
disp('Index Value');
46+
i %index;
47+
end
48+
%%
49+
% In the previous example, we just showed the values of *Sinx* and *did NOT
50+
% save* them anywhere
51+
%
52+
% Now define *y matrix* and store the *Sin(x) values* by defining index for
53+
% the y matrix
54+
55+
% perform your work here
56+
y=[];
57+
for i=1:s(1,2)
58+
y(i)=sin(x(1,i));
59+
end
60+
%%
61+
% Now Plot x vs y and set xlabel and ylabel as *X* and *Y* and *title* as *Sinx*
62+
%
63+
% Set axis between *x=[-pi,pi] , y=[-1,1]*
64+
%
65+
% Create a box for your plot
66+
%
67+
% and hold on the plot to keep it for the next step
68+
%
69+
% *Optional*: plot it with red markers and dashed lines.
70+
71+
plot(x,y,"r-");
72+
hold on
73+
xlabel('X')
74+
ylabel('Y')
75+
axis([-7,pi,-pi,pi]);
76+
box on
77+
title('Sinx')
78+
79+
%% Exercise 2
80+
% Redo the excersie 1, now for Cosx and store the data in the y1 matrix
81+
82+
y1=[];
83+
for i=1:s(1,2)
84+
y1(i)=cos(x(1,i));
85+
end
86+
% Change the title to 'Sinx & Cosx'
87+
% put a legend to specify Cos x and Sin x
88+
%
89+
% *Optional*: plot it with blue markers and dashed lines.
90+
91+
plot(x,y1,"b--");
92+
legend('Sin x', 'Cos x')
93+
%% Exercise 3
94+
% use viscircles(centers,radii) to draw a circle at position [0,0] and with
95+
% radius of 0.05
96+
97+
viscircles([0,0],0.05)
98+
%%
99+
% Create a *for loop* to add *10 more* circles in the same distance within x=[-pi,pi]
100+
% on the *Sinx* figure
101+
%%
102+
% # define the x values
103+
104+
x=linspace(-pi,pi,10)
105+
%%
106+
% 2. define the y values corrospond to x values using for loop
107+
108+
y3=[];
109+
for i=1:10
110+
y3(i)=sin(x(1,i));
111+
end
112+
%%
113+
% Create another for loop to plot t circle with calculated x and y
114+
115+
for i=1:10
116+
viscircles([i,sin(y3(i))],0.05);
117+
end
118+
legend('Sinx', 'Cosx')
119+
%%
120+
%
121+
%% Exercise 4
122+
% Defining a function
123+
% define a function at the end of the code and name it *myfun*. Get two inputs
124+
% and multiply them by two and show them as two outputs
125+
%
126+
% Test your *myfun* function with two numbers
127+
128+
myfun(1,3)
129+
%%
130+
%
131+
132+
function [y1,y2] = myfun(x1,x2)
133+
134+
y1=x1*2;
135+
y2=x2*2;
136+
disp(y1);
137+
disp(y2);
138+
end

lab05_t2_e1_labA/prelab05.mlx

47.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)