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 : 2 a , 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
0 commit comments