900 likes | 1.16k Views
Graphic Objects figure axes 2D-plot 3D-plot axis labels title GUI objects pushbutton toggle edit text menu. Continuing with our GUI project. Sliders. Sliders provide an easy way to gradually change values between a given range. Three ways to move the slider. Sliders.
E N D
Graphic Objects • figure • axes • 2D-plot • 3D-plot • axis labels • title • GUI objects • pushbutton • toggle • edit • text • menu
Sliders • Sliders provide an easy way to gradually change values between a given range. Three ways to move the slider
Sliders • The user has three possible way to change the position of the slider • Click the arrow buttons => small value changes • Click the trough => large value changes • Click and drag the slider => depends on user • The default changes are 1% and 10%
We must set a value between Min and Max Sliders • The range of the slider is defined with the Min and Max uicontrol properties. • The amount of change related to an user click is controlled with the SliderStepproperty which is a two element vector ([0.01 0.1] default) hToVertSlider= uicontrol('Style','slider',... 'Position',[160 10 20 120],... 'Min',10,... 'Max',20,... 'Value',15,... 'SliderStep',[0.1 0.25],... 'Callback','')
Sliders • Given the setting from Min, Max and SliderStep the amount of change to the current Value are as follow: • For an arrow click: • Value = Value + SliderStep(1) * (Max – Min) • 16 = 15 + 0.1 * (20 - 10) • For a trough click: • Value = Value + SliderStep(2) * (Max – Min) • 17.5 = 15 + 0.25 * (20 - 10)
Step V– the speed slider uicontrol('style' ,'slider',.... 'position',[130 10 450 30],... 'tag' ,'speedSlider',... 'max' ,pi/2,... 'min' ,0.01,... 'value' ,pi/4);
Step V– the speed slider uicontrol('style' ,'slider',.... 'position',[130 10 450 30],... 'tag' ,'speedSlider',... 'max' ,pi/2,... 'min' ,0.01,... 'value' ,pi/4); What about the callback?
Text and editable text • Static texts are commonly used to give instructions or to display other controllers values (such as sliders) • …’Style’,’text’,… • Static text can not execute callbacks. • Editable texts gets string input from the GUI user. • When the user enters the edit field and change its content, only the String property is affected. • Editable text can execute callbacks
Step VI – the text frame uicontrol('style' ,'text',... 'string' ,'C',... 'tag' ,'C_title',... 'position' ,[490 290 60 70],... 'backgroundColor','y'); What about the callback?
Step VII – the edit window uicontrol('style' ,'edit',... 'string' ,'1',... 'value' ,1,... 'tag' ,'C',... 'position',[500 300 40 40],... 'callback', {@getC}); function getC(calling_button, eventData) s = get(calling_button,'string'); set(calling_button,'value',str2double(s)); end
Selected Radio Buttons • Radio buttons are similar to checkboxes but designed to specify options that aremutually exclusive like inmultiple-choice questions hToRadio1 = uicontrol('Style', 'radio',... 'Position',[20 300 120 20],... 'String','Option1',... 'Callback', {@turnOffTheOtherButtons},... 'Value',1)
Function turnOffTheOtherButtons h = findobj('Tag','option1'); set(h,'Value',0); : : Do we need a different function for each button?
Step VIII – the radio buttons uicontrol('style' ,'radio',... 'string' ,'surf',... 'callback', {@plotTypeButtons},... 'tag' ,'plot_type',... 'position',[490 250 80 30],... 'value' ,1,... 'userdata',1); uicontrol('style' ,'radio',... 'string' ,'contour3',... 'callback', {@plotTypeButtons},... 'tag' ,'plot_type',... 'position',[490 220 80 30],... 'value' ,0,... 'userdata',0);
Step VIII – the radio buttons function plotTypeButtons(calling_button, eventData) handles = findobj('tag','plot_type'); set(handles,'value',0); set(calling_button,'value',1); end
Step IX - Now lets use it function run(main_axes, small_axes) onOff = findobj('tag','onOff'); speedSlider = findobj('tag','speedSlider'); C_window = findobj('tag','C'); close_button = findobj('tag','close'); type_handels = findobj('tag','plot_type');
Step IX - Now lets use it function run(main_axes, small_axes) onOff = findobj('tag','onOff'); speedSlider = findobj('tag','speedSlider'); C_window = findobj('tag','C'); close_button = findobj('tag','close'); type_handles = findobj('tag','plot_type'); How does type_handles differ from the other handles?
kill = get(close_button,'userData'); [x y] = meshgrid(-2:0.2:2); t = 0.001; while (~kill) on = get(onOff,'userData'); C = get(C_window,'value'); speed = get(speedSlider,'value'); kill = get(close_button,'userData'); activeB = findobj(type_handels,'value',1); plotType = get(activeB,'userdata'); if (on) draw(x,y,main_axes,small_axes,t,C,plot_type); t = t + speed; if (t >= 2*pi) t = 0.0001; end end pause(0.1); end
kill = get(close_button,'userData'); [x y] = meshgrid(-2:0.2:2); t = 0.001; while (~kill) on = get(onOff,'userData'); C = get(C_window,'value'); speed = get(speedSlider,'value'); kill = get(close_button,'userData'); activeB = findobj(type_handels,'value',1); plotType = get(activeB,'userdata'); if (on) draw(x,y,main_axes,small_axes,t,C,plot_type); t = t + speed; if (t >= 2*pi) t = 0.0001; end end pause(0.1); end
kill = get(close_button,'userData'); [x y] = meshgrid(-2:0.2:2); t = 0.001; while (~kill) on = get(onOff,'userData'); C = get(C_window,'value'); speed = get(speedSlider,'value'); kill = get(close_button,'userData'); activeB = findobj(type_handels,'value',1); plotType = get(activeB,'userdata'); if (on) draw(x,y,main_axes,small_axes,t,C,plot_type); t = t + speed; if (t >= 2*pi) t = 0.0001; end end pause(0.1); end
kill = get(close_button,'userData'); [x y] = meshgrid(-2:0.2:2); t = 0.001; while (~kill) on = get(onOff,'userData'); C = get(C_window,'value'); speed = get(speedSlider,'value'); kill = get(close_button,'userData'); activeB = findobj(type_handels,'value',1); plotType = get(activeB,'userdata'); if (on) draw(x,y,main_axes,small_axes,t,C,plot_type); t = t + speed; if (t >= 2*pi) t = 0.0001; end end pause(0.1); end
kill = get(close_button,'userData'); [x y] = meshgrid(-2:0.2:2); t = 0.001; while (~kill) on = get(onOff,'userData'); C = get(C_window,'value'); speed = get(speedSlider,'value'); kill = get(close_button,'userData'); activeB = findobj(type_handels,'value',1); plotType = get(activeB,'userdata'); if (on) draw(x,y,main_axes,small_axes,t,C,plot_type); t = t + speed; if (t >= 2*pi) t = 0.0001; end end pause(0.1); end fig = findobj('tag','the_figure'); close(fig); end
Step XI – Adding color map menu cm = uimenu('tag','colorMap','label','Color Map');
Step XI – Adding color map menu cm = uimenu('tag','colorMap','label','Color Map'); uimenu(cm, 'label','jet', ‘callback',{@set_colorMap,jet}); uimenu(cm, 'label','hot', 'callback',{@set_colorMap,hot}); uimenu(cm, 'label','cool', 'callback',{@set_colorMap,cool}); function set_colorMap(calling_manu, eventData,colorMap) set(gcf,'colorMap',colorMap); parent = get(calling_manu,'parent'); all = get(parent,'children'); set(all,'checked','off'); set(calling_manu,'checked','on'); end
Checkboxes • Checkboxes allow the user to turn on/off a number of independent options hToCheckBox = uicontrol('Style', 'checkbox',... 'Position',[20 450 120 20],... 'String','Check Box example‘, ... ‘Callback’,’’) • Note: the position is measured in pixels from the left-bottom corner of the figure
Basics of Linear Algebra start with two examples: In 1st : We do not need Linear Algebra In 2nd : Linear Algebra is needed
Consider a species where N represents number of individuals. This species reproduces with a very simple rule: in any generation t (t = 0,1,2,....), the population changes its size by a factor of R. Hence, at t+1 size of population is N(t+1) = R N(t). Question: if we start with a population of size N(0), what will be its size after t generations?
Numerical computation (for two possible values of R)- gen=[1 2 3 4 5 6 7 8 9 10]; N0=100; %initial condition for populations n1=N0; R1=1.1; %Params for population 1 n2=N0; R2=0.9; %Params for population 2 for t=1:10 % numerical computation P1(t) = n1; n1 = n1*R1; %change pop 1 P2(t) = n2; n2 = n2*R2; %change pop 2 end plot(gen,P1,'or:',gen,P2,'^b-');
P1 P2 if R>1, the population grows to infinity. if R<1 it decays to 0.
Back to Question: if we start with a population of size N(0), what will be its size after t generations? In our case, the answer is trivial – no need for numerical computation: For every t N(t+1) = RN(t) Hence- N(t) = Rt N(0). In our example- N1(10) = 1.110 * 100 = 2.59 *100 = 259 N2(10) = 0.910 * 100 = 0.35 *100 = 35 In this case, there is an analytical solution
Simple problems have analytical solutions Complex problems can be solved only with numerical computations
For example, consider a species in which parameters are age-dependent. • Reproduction- • 1) age=0 (from birth to the age of 1) does not reproduce • 2) age=1 yields 2 offsprings • 3) age=2 yields 1.5 offsprings • Survival- • 4) 40% of age 0 survive to next generation • 5) 30% of age 1 survive to next generation • 6) 10% age 2 survive to next generation • 7) No females of age 3 survive.
We may want to ask a questions like: what will be N(T) Here the answer is more complex, because the system is defined by several equations, not only one:Let Ni denote the number of individuals of age i.N0(t+1) = 2N1(t)+1.5N2(t)N1(t+1) = 0.4N0(t)N2(t+1) = 0.3N1(t)N3(t+1) = 0.1N2(t) N(t+1) = N0(t+1)+N1(t+1)+N2(t+1)+N3(t+1) We may want to ask additional questions like: when will the fraction of newborn stay constant, i.e : Find t such that N0(t)/N(t) = N0(t+1)/N(t+1) ?
To solve this problem, we need techniques from Linear Algebra.Start by reviewing important concepts in Linear Algebra.Will come back to this problem later, after learning some Linear Algebra
Matrix addition/substraction Suppose A=[ai,j] and B=[bi,j] are two m x n matrices. Then C=A+B is an m x n matrix where ci,j = ai,j+bi,j for 1 im, 1j n Example-
Matrix addition/substraction Example with 3 matrices-
Matrix Multiplication- Suppose A=[ai,k] is an mby lmatrix and B =[bk,j] is an lby n matrix. Then: C=A B is an m by n matrix with: This is small L in Italics In other words, in matrix C an element at place i,j is the result of multiplying the elements of row i in A by the elements of column j in B, and sum them up.
Example- The multiplication is valid, because the number of columns in A is equal to the number of rows in B. The result is a matrix with 2 rows (number of rows in A) and 4 columns (number of columns in B). To find any element cij, we multiply the elements of the ith row in A by the elements of the jth column of B, and sum them- Third column elements in B Second row elements in A
In Matrix multiplication, the order is important ! in other words, AB BA: • Example:
In Matrix multiplication, the order is important ! in other words, AB BA: • Example:
In Matrix multiplication, the order is important ! in other words, AB BA: • Example where we reverse order of multiplication: (from previous slide)
Check that you did it right Suppose A=[ai,k] is an mby lmatrix and B =[bk,j] is an lby n matrix. Then: C=A B is an m by n matrix with: Test 1 Test 1: Rows of first matrix = columns in second matrix
Check that you did it right Suppose A=[ai,k] is an mby lmatrix and B =[bk,j] is an lby n matrix. Then: C=A B is an m by n matrix with: Test 2 Test 1: Columns of first matrix = rows in second matrix Test 2: dimensions of resulting matrix are the rows of first and columns of second
If you multiply an array by a vector If the vector is a column vector – Always the vector must be on the right hand side C=AxB A B C=BxA Or the vector must be a row vector
Writing a system of linear equations in matrix form Consider the following system of n equations with n variables xi, i=1..n. It is convenient to represent this system of equations in matrix form: Vector of Right Hand Side (RHS)Values Coefficient Matrix Matrix Multiplication Operator Vector of variables
The system above can be also written in short: Vector of Right Hand Side (RHS)Values Coefficient Matrix Vector of variables Matrix Multiplication Operator Or simply-
Power of Matrices Note- A must be a square matrix !!! Otherwise multiplication is undefined So far – Linear Algebra. Let’s move back to MATLAB
Summary of what you know about Matrices in MatLab 1) A matrix is entered row-wise, with consecutive elements of a row separated by a space or a comma A=[ 1 2 3 ] A=[ 1, 2, 3 ] 2) Rows are separated by semicolons or carriage returns. B= [ 1 2 3 4 5 6 ] 3) Entire matrix must be enclosed within square brackets A=[ 1 2 3 ] 4) A single element of the matrix is accessed by specifying its index(ices), in round brackets. B(2,3)=6 5) Vector - a special case of a matrix. Can be column vector, or row vector 6) Scalar is a single element. It does not need brackets. 7) A null matrix is a matrix with no elements. It is defined by empty brackets []
>> A=[ 1 2 5; 3 9 0] • A = • 1 2 5 • 3 9 0 >> u = [1 3 9] % row vector u = 1 3 9 >> v = [1; 3; 9] % column vector v = 1 3 9 >> transpose(v) % for convenience ans = 1 3 9 >> (v)’ % easier way to transpose ans = 1 3 9