This document describes analyzing a half-wave rectifier circuit using MATLAB. It includes:
1. An introduction to MATLAB and the assignment requirements to plot the voltage across the resistor compared to the source voltage.
2. Design and flowcharts for the MATLAB code, including getting input values, running a for loop over time, and switching between "diode-on" and "diode-off" states.
3. Results and analysis from running the code, including comparing outputs to Multisim simulations and developing a graphical user interface.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100%(2)100% found this document useful (2 votes)
2K views33 pages
Half Wave Rectifier Circuit Simulation
This document describes analyzing a half-wave rectifier circuit using MATLAB. It includes:
1. An introduction to MATLAB and the assignment requirements to plot the voltage across the resistor compared to the source voltage.
2. Design and flowcharts for the MATLAB code, including getting input values, running a for loop over time, and switching between "diode-on" and "diode-off" states.
3. Results and analysis from running the code, including comparing outputs to Multisim simulations and developing a graphical user interface.
1. Introduction: MATLAB (matrix laboratory) is a numerical computing environment which is developed by Mathworks. Matlab allows a lot of mathematical operations on matrixes with plotting the resultant data with 2-D and 3-D graphs. This software can solve and simulate a lot of mathematical and engineering problems and for enhancement; it accepts many toolboxes to be installed to enhance its abilities to do many different engineering operations. Matlab was first developed in the late 1970s by Cleve Barry Moler, the head of computer science department in the University of New Mexico. Once this software was able to be used be people, the number of softwares users was increasing significantly and not only individual users started using it, but most of the universities were exposed to it in a short time. And in this assignment, Matlab will be used in order to analyze a half-wave rectifier circuit and by stating the formulas it plots the resultant outputs for the required demands. (Mathworks.com, 2013)
Its known that Matlab is numerical software and needs an Electrical or Analog electronics Toolbox in order to draw a circuit diagram and then analyze it. But since there is no any toolbox used for this assignment, the half-wave rectifier circuit should be analyzed with deriving its input/output relation formula in order to simplify the circuit to be numerically understood by Matlab. This assignment is consisted of 3 main parts; each of them has its own deliverables. In part A, it is required to plot the voltage across the resistor and superimpose the source voltage in the same graph in order to compare and clarify how the resistors voltage gets affected by the supply voltage and by other circuit components. For verification, similar circuit was implemented in Multisim by using the Oscilloscope function and both Multisim and Matlab Results were compared. Part B, is a simple input test for part A. certain values for the circuit components were given and it was required to use it to plot the voltage across the resistor comparing to the source voltage. Then In C, the codes got more complicated, since the codes should not accept any non-numerical value for the inputs and also there were minimum and maximum limits for both voltage and resistance entries. Engineering Software & Applications 4
Finally, part D, was requiring to develop a Graphical User-Interface (GUI) which consists of figure window containing menus, buttons, text, graphics, etc., This GUI will allow the user to manipulate interactively with the mouse and keyboard. (Shang, 2012). 1. Design and Flow: 1 i T END TRUE state = 'Diode-On' If (Ic+Ir) 0 TRUE ta = t(i) state = 'Diode-Off' Vi(i) >= Vr (i) FALSE FALSE TRUE Vr(i) = Vi; Ir = Vi / R; Ic = w * C * Vo * cos(w * t(i)); i = i+1 i = i+1 Vr(i) = Vo* sin(w * ta).* exp(- (t(i)-ta)./(R*C)); T = 0: 0.05*10^-3:70*10^-3 i = number of elements of T Enter; Voltage, Resistance , Capacitance, Frequency
2. 1 : Task 1 A Flowchart
Engineering Software & Applications 5
The codes start by asking the user to enter the values of Voltage, Resistance, Capacitance and Frequency, then the capacitance value will be converted into micro Farad [uF] and all other values will remain as been entered. The time [t] was defined as requested in the assignment question and then a variable (T) was assigned to be equal to the number of elements of array[t]. Switch statement was used to fluctuate between the two states of the circuit, and the state was initialized to be Diode-ON. Then, for loop were used and assigned to be repeated by [T] times. [T] Indicates the number of elements of the array [t]. Once Diode-On state gets executed, it applies some identified formulas such as calculating the currents through the resistor and capacitor and checks if the sum of the two currents is less or equals to zero, if this condition is detected it will assign the state to be Diode-OFF and apply another formula of the current through resistor, and by each increment of time it checks if the value of the input voltage becomes more than or equals to the resistors voltage, if this condition is detected the state gets switched to be Diode-ON and the For loop keeps it running till it reaches the looping limit [T], which it was defined to be the maximum elements column in the time array [t]. START Enter Vo Vo = Numeric 10 Vo 20 TRUE B TRUE B Enter R R = Numeric 1500 R 2500 TRUE FALSE FALSE FALSE FALSE C TRUE C Enter F F = Numeric TRUE FALSE D D C = 10*10^-6 : 10*10^-6 : 50*10^-6 Plot (Vr,Vs) [Part(A)] END
2. 2 : Task 1, C flowchart.
Engineering Software & Applications 6
[Task 1 C], has some similarities of [Task 1 A] but it has more conditions to be checked before executing the previous defined codes in [Task 1 A] for every entry of any of the parameters, the codes do convert the entered string to numbers and any non-numerical element becomes null[empty]. Accordingly, the codes do look for any empty space in the converted entered array by using the logical function [isempty]. If theres any space detected, the logical function gives a true value [1], on the other hand it gives a false value [0]. By using the previous sequence, all inputs were analyzed and determined whether they are numerical or not, and accordingly if any input was non-numerical, While loop was used to keep asking the user for a new entry until the user enters a numerical value. When a numerical value is entered, the codes do check if the entered numerical values are in the defined range. In the case when an entered value is out of the defined range, theres a while loop keeps running and asking to renter again, and I wont go forward unless if the user entered a value in within the requested range. After all, the code defined the value of capacitance as requested in the assignment question, and then substituted for all entered parameters into the Codes defined in [Task 1 A]. Home GUI Main GUI About Rectifiers Video On Rectifiers Explanation Graphs and Definiti ons Enter Values 1500 < R < 2500 10 < Vo < 20 Warning Vo,R Plot (Vs,Vr) TRUE FALSE END
There are 4 different Graphical User-Interfaces. The first one is the [Home GUI] which relates all GUIs together. The second one [About Rectifiers GUI] it contains some charts and it links to another GUI that shows a video about rectifiers. The Main GUI, is the GUI were the voltages been plotted in. Before plotting, the [MAIN GUI] checks for the entered values of resistance and voltage and compares it with a certain limits that were defined by the assignment question and also checks whether these values are numeric or non-numeric value and shows a warning if any of the statements is detected. 2. Results and Analysis: As the codes of [Task 1] A were done, it was tested according the requirements of [Task 1 B] as shown in the next figure:
3. 1 : Testing Task 2 B ' Command Window shot. And the resultant plot is shown in the next figure:
According to the inputs in the previous figure, the resultant plot is as shown in the next figure:
As shown in figure 2.2 there are four different graphical user interfaces one is the home GUI which links all the GUIs together as shown in the next figure:
As shown in the previous figure, the Home GUI has two main buttons; one is directly taking the user to the main GUI that plots the waveform of the voltage. And another one which opens another GUI called [About]. It also has two main axises that show different photos, one is showing a simple half-wave rectifier circuit and the other one is showing the logo of our university APU, and finally there is a 1-element-menu which takes the user directly to the [ABOUT GUI] The Second GUI is the [ABOUT GUI] which is shown in the next figure:
3. 7 : [ABOUT] Graphical User-Interface. The GUI shown previously shows a graph that visually explains how a rectifier works and how the resultant voltage across the resistor will be. And also it explains in a paragraph some basic theoretical about half-wave rectifiers. The next video GUI shows an animation video that demonstrates about rectifiers as shown in next figure:
The last GUI is the GUI which plots the required waveform for the indicated voltage across the resistor:
3. 9 : The main GUI.
The Main GUI shown in the last figure is taking the value of Vo, R, Frequency pop-up, and slider and then compares the entered values with some identified condition if any of the identified limits were crossed, it shows an error as shown in next 4 figures:
Some enhancements were added to the main GUI in order to make it easier to be used. Value inputs and indicators were organized inside panels and also background photo was added to all the GUIs. Capacitor variation displayer was added and programmed to show he value of capacitor whenever it changes, one more function of this displayer is that the user can DoubleClick on it and use the keyboard to enter the value of capacitance so this text box can be used for viewing and editing the value of capacitance. One more Enhancement was added to main menu to suggest the user on high and one low choice of the values of all parameters. The toolbar is been also enhanced by enabling the user to zoom in/out the plot and also to rotate the graph, also one new button was added to the toolbar which sets the frequency to be 60Hz as it was called and tagged be { Hz SI [System International Frequency] }. (Higham and Higham, 2005.
Engineering Software & Applications 13
3. Discussion: Rectifiers are known that it functions converting AC Voltage to DC by neglecting the negative half of the sinusoidal wave. The existence of capacitor in parallel with the resistor will help reducing the fluctuation of the sinusoidal wave because it discharges once the diode stops the negative current of flowing. And the bigger value of capacitance the more stable the voltage becomes. The Codes were written to go with the same flow which the real rectifier works under a predefined period of time, two states were designed to simulate the same states in reality and a loop was defined and kept running for a certain defined period. (EETimes, 2013) In the first part of the codes, T variable was assigned to be the number of elements of the defined function of time [t], and then stated the diode to be initialized to be under the On-state. Assuming [i] to be an array starts by 1 and ends up with the number of elements in the array T which is previously stated to be the number of elements in the defined period, by using the previous sequence, we enable the script to apply the given formula in each assigned increment of time. While and For loops has the same function, but in details; For loops is used when there is a known number of iterations and While loops is best used when there is unknown number of repetitions and since the number of loop repetition is starting by an initial state and goes till reaching a limit. So, For loop statement will be used since this limit is known. Since there are two states for the circuit, the best way to present this situation is by using Switch Statement which enables the script to switch the diode state according to the stated formulas which were defined under each state. During the On-state, in every time the voltage source and resistors voltage gets calculated, and after every substitution in these formulas it checks the condition which says [If Ir + Ic<=0], if it is true it will do a certain statement which is assigning the new state to be off. And accordingly it starts the OFF-state and keeps calculating an checking another condition until it becomes true, it assigns the state be On-state again. (Smith, 2010)
Engineering Software & Applications 14
For Part C, three while loop staments were used in order to check the type of input and its range. One while loop will run if the entered input is not numerical and gets neglected if the input was a number. Another while loop will run if the entered input is numerical and is out of the identified range, it keeps asking the user to enter new values in that specific range. For more precaution, one more loop was added under the second loop, and it operates the checking the type of the second entered value, if the user entered any non-numerical value, another message will appear asking to enter a value of certain range. By this sequence the codes will never allow any non-numerical value or any out of range value to be entered and thats because While loop is the used loop, which has a function that keeps checking the condition and do the statement once there is any detection for that condition. In part D, once the plot button is clicked, it will gather the input parameters values from the specified locations and [If, elseif] statement was used in order to check is any of the input parameters is exceeding the range or is not numerical values, it will check the entered parameters one by one and an error message will appear if there is any exceedance, If there is any exceedance, the codes will stops the sequence of the codes in order to let the user enter again and not to let the codes plot any unwanted waveform. For analyzing the obtained results of the main GUI, differing the input parameters will differ the flow of the waveform and therefore, next points were concluded: If there were no capacitor, the voltage across the resistor will repeat the same as the supply voltage but with a voltage [0v] when voltage source start becoming negative. The bigger value of capacitance, the more that Vr becomes more stable to be DC voltage with Vo amplitude. The value of resistance may affect the stability of the Vr fluctuation. As a circuit design tip, there should be high value capacitor that its discharging current can be enough to stable the voltage across the resistor.
Engineering Software & Applications 15
4. Conclusion: To put is in a nutshell, the objective of this assignment was successfully achieved and all required tasks were done without any errors. A lot of skills were obtained and many problem solving techniques were comprehended. Usage of different logical functions and some programming statements such as If statements, switch statement and while and for loops were all used and explained. A lot of GUI designing and programming skills were gained and a perfectly working multiple GUIs were done and tested successfully with a lot of added enhancements. After all, the importance of using MATLAB in the next years of studying and working was realized and also the relationships among the programming languages and software and being familiar with all programming languages. In conclusion, special thanks for Dr. THANG KA FEI for teaching us this module and guiding us to search and think about doing each part of the assignment and also encouraging us to do more enhancements.
Engineering Software & Applications 16
5. References:
EETimes. 2013. Matlab edges closer to electronic design automation world | EE Times. [online] Available at: http://www.eetimes.com/document.asp?doc_id=1151422 [Accessed: 16 Sep 2013]. Higham, D. and Higham, N. 2005.MATLAB guide. Philadelphia: Society for Industrial and Applied Mathematics. Mathworks.com. 2013. MATLAB - The Language of Technical Computing. [online] Available at: http://www.mathworks.com/products/matlab/ [Accessed: 16 Sep 2013]. Shang, W. 2012. We1 5hang: MATLAB GUI Inserting background image. [online] Available at: http://we15hang.blogspot.com/2012/02/matlab-gui-inserting-background- image.html [Accessed: 27 Sep 2013] Smith, D. 2010. Engineering computation with MATLAB. Boston: Addison/Wesley.
Engineering Software & Applications 17
6. Appendix: 6.1. Codes for Task1 A. 7. % Asking the user to enter a values. 8. Vo = input('Enter the Source Voltage[v] : '); 9. R = input('Enter the resistance[Ohms] : '); 10. c = input('Enter Capacitance Value[uF] : '); 11. F = input('Enter the frequency[Hz] : '); 12. % Converts the capacitance value to be in the range of (Micro). 13. C = c/1000000; 14. % omega = 2 x pi x Frequency 15. w = 2 * pi * F; 16. t = 0 : 0.00001 : 0.07; 17. % Calculating the Input voltage according to all sinosoidel waves. 18. T = length(t); 19. % Assigning T to the number of vector[t] elements. 20. state = 'Diode-On'; 21. % assigning the initial state to be "Diode-on". 22. for i = 1 : T 23. Vi = Vo * sin(w * t); 24. % Calculating the source voltage for the circuit. 25. switch state 26. case 'Diode-On' 27. Vr(i) = Vi(i); 28. % When diod is ON, the voltage accross the resistor is equal to 29. % the source voltage. 30. Ir = Vi(i) / R; 31. % According to Ohms's Law [Current = Voltage / Resistance]. 32. Ic = w * C * Vo * cos(w * t(i)); 33. % Caluculating the current through capacitor as given. 34. A = Ir + Ic; 35. % if the sum of Ir and Ic becomes less than or equlas to 0, it 36. % changes the state to be Diode-Off 37. if A <= 0 38. state = 'Diode-Off'; 39. Ta = t(i); 40. end 41. case 'Diode-Off' 42. Vr(i) = Vo* sin(w * Ta).* exp(-(t(i)-Ta)./(R*C)); 43. % if the supply voltage becomes larger or equlas to Vr it 44. % changes the state to be Diode-On 45. if Vi(i) >= Vr (i) 46. state = 'Diode-On'; 47. end 48. end 49. end 50. % Plots the supply voltage with red color 51. plot(t , Vi , 'r'); 52. % hold plotting in order to superimpose both plots together 53. hold on Engineering Software & Applications 18
54. % Plots the resistors voltage with blue color 55. plot(t , Vr , 'b--'); 56. hold off 57. % labels x as shown; 58. xlabel(' Time (s) '); 59. % labels y as shown; 60. ylabel(' Voltage (V) '); 61. % enable the user to differentiate between the 2 plots 62. legend (' Supply Voltage ', ' Resistors Voltage '); 63. title(' Comparing the voltage supply to the resistors volage ');
Codes for Task 1 C %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %------------------------------------------------------------------------%
% Asking the user for entering a value. Followed by ['s'] to let it recognize % alphabetic letters Vo = input ('Enter the Source Voltage [10-20]v: ', 's' ); % As the user enter values, it gets coverted to numeric array. Vo = str2num(Vo); % Assigning X to be true, if the array of the input has any [empty{Non- numeric}] element. X = isempty(Vo); % runs this loop if the entered input is non-numerical. while X == 1 Vo = input ('Please Enter A Numerical Value !! : ', 's' ); Vo = str2num(Vo); % keeps running the loop till a numeric value gets entered. X = isempty(Vo); end % runs this loop if the entered vlaue is out of the required range. while Vo < 10 || Vo > 20 Vo = input('The Voltage Value Should be between [10-20]v: ','s'); Vo = str2num(Vo); X = isempty(Vo); % checks whether the second input of the user is numerical or not. while X == 1 Vo = input ('Please Enter A Numerical Value !! : ', 's' ); Vo = str2num(Vo); X = isempty(Vo); end end
R = input ('Enter The Resistance value [1500-2500]Ohms: ', 's' ); R = str2num(R); % Assigning Y to be true, if the resistance input is not numerical. Y = isempty(R); % runs this loop if the entered input is non-numerical. while Y == 1 R = input ('Please Enter A Numerical Value !! : ', 's' ); R = str2num(R); Engineering Software & Applications 19
Y = isempty(R); % keeps running the loop till a numeric value gets entered. end % runs this loop if the entered vlaue is out of the required range. while R < 1500 || R > 2500 R = input('The Resistance Value Should be between [1500-2500]Ohms!! : ','s'); R = str2num(R); Y = isempty(R); while Y == 1 R = input ('Please Enter A Numerical Value !! : ','s'); R = str2num(R); Y = isempty(R); end end
F = input ('Enter the Frequency [Hz]: ', 's' ); F = str2num(F); Z = isempty(F); % Assigning Z to be true, if the frequency input is not numerical. while Z == 1 F = input (' Please Enter A Numerical Value !! '); F = str2num(F); Z = isempty(F); end while F <= 0 % this statment will run if the frequency input is <= 0. F = input('Frequency should be a positive value !!. Enter Again : ', 's'); F = str2num(F); Z = isempty(F); while Z == 1 F = input (' Please Enter A Numerical Value !! '); F = str2num(F); Z = isempty(F); end end
w = 2 * pi * F; % Omega= 2 x Pi x Frequency. FP = 70 * 10^-3 ; % Full period. t = 0 : 1 * 10^-4 : FP; % from starting time - to the period time with an increment of 0.0001s . C = 10 * 10^-6 : 10 * 10^-6 : 50 * 10^-6; % testing the codes with 5 different values of capacitors.
T = length(t); state = 'Diode-On'; % Initiallizing the state to be `On` for Nc= 1:5 % As there'r 5 capacitance values for i = 1:T % Assigning i to be equal to the number of elements in vector [t] switch state case 'Diode-On' Vi( i , Nc ) = Vo * sin( w * t(i) ); Engineering Software & Applications 20
Vr( i , Nc ) = Vi( i , Nc ); Ir = Vi( i , Nc ) / R; % simple [Ohms law] Ic = w * C(Nc) * Vo * cos( w * t(i) ); % Calculates the capacitor's current for the 5 different % capacitors in every increment of time S = Ir + Ic; % As the sum of currents is zero or less, means that diode is off % state because no current is flowing. if S <= 0 state = 'Diode-Off'; Ta = t(i); % Ta is the time when the diode is off, and it is % consiered as a part of [t] end case 'Diode-Off' Vi( i , Nc ) = Vo * sin( w * t(i) ); % Calculates the source Voltage Vr( i , Nc ) = Vo * sin( w * Ta ).* exp(-(t(i) - Ta )./( R * C(Nc) )); % As given; the voltage accross resistor when the diode is % off if Vi( i , Nc ) >= Vr( i , Nc ) % Once the resistor's voltage becomes same or less than % the source voltage , it means that the diode is on % again state = 'Diode-On'; end end end end
plot(t,Vi(:,1),'r:'); % plots all rows , first colom of matrix [Vi] hold on % Enabling to superimpose more than a plot in one figure. plot(t,Vr); % plots Vr hold off xlabel('Time (s)'); % label x axis ylabel('Voltage (V)'); % label y axis title(' Comparing the voltage supply to the resistors volage'); % used in order to distiguish among different voltages legend(' Supply Voltage ','Vr,C = 10 uF','Vr, C = 20 uF','Vr, C = 30 uF','Vr, C = 40 uF','Vr, C = 50 uF');
Codes for Task 1 D [Home GUI] function varargout = HomeGUI(varargin) % HOMEGUI MATLAB code for HomeGUI.fig % HOMEGUI, by itself, creates a new HOMEGUI or raises the existing % singleton*. % % H = HOMEGUI returns the handle to a new HOMEGUI or the handle to % the existing singleton*. % % HOMEGUI('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in HOMEGUI.M with the given input arguments. % % HOMEGUI('Property','Value',...) creates a new HOMEGUI or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before HomeGUI_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to HomeGUI_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help HomeGUI
% Last Modified by GUIDE v2.5 27-Sep-2013 21:57:33
% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @HomeGUI_OpeningFcn, ... 'gui_OutputFcn', @HomeGUI_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end
if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT
% --- Executes just before HomeGUI is made visible. function HomeGUI_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to HomeGUI (see VARARGIN)
Engineering Software & Applications 22
%% reads 2 different photos and shows it in an axises. Logo = imread('APUlogo.png'); pic = imread('HOMEPIC.png'); %% assigning axes1 to be the axes where to plot the photo. axes(handles.axes1) %% show the photo in the assigned axes. imshow(Logo) %% assigning axes2 to be the axes where to plot the photo. axes(handles.axes2) %% show the photo in the assigned axes. imshow(pic)
%% creates an axes which covers the whole GUI BG = axes('unit', 'normalized', 'position', [0 0 1 1]); %% importing the background photo. BackGround = imread('BuiltInBorad.png'); imagesc(BackGround); %% prevent plotting over the background and turn the axis off set(BG,'handlevisibility','off','visible','off')
%% To makes the background behind everything. uistack(BG, 'bottom'); %% code source :(Shang W., 2012)
handles.output = hObject; % Choose default command line output for HomeGUI guidata(hObject, handles); % Update handles structure % UIWAIT makes HomeGUI wait for user response (see UIRESUME) % uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line. function varargout = HomeGUI_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure varargout{1} = handles.output;
% --- Executes on button press in MainGUI. function MainGUI_Callback(hObject, eventdata, handles) % hObject handle to MainGUI (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in About. function About_Callback(hObject, eventdata, handles) % hObject handle to About (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
%% opens the about GUI if the button is clicked About;
% ------------------------------------------------------------ function AboutMenu_Callback(hObject, eventdata, handles) % hObject handle to AboutMenu (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
%% opens the about GUI if the button is clicked About;
Codes for Task 1 D [About GUI] function varargout = About(varargin) % ABOUT MATLAB code for About.fig % ABOUT, by itself, creates a new ABOUT or raises the existing % singleton*. % % H = ABOUT returns the handle to a new ABOUT or the handle to % the existing singleton*. % % ABOUT('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in ABOUT.M with the given input arguments. % % ABOUT('Property','Value',...) creates a new ABOUT or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before About_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to About_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help About
% Last Modified by GUIDE v2.5 27-Sep-2013 23:10:42
Engineering Software & Applications 24
% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @About_OpeningFcn, ... 'gui_OutputFcn', @About_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end
if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT
% --- Executes just before About is made visible. function About_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to About (see VARARGIN)
%% reading a photo from the current folder. HalfWaveRectifier = imread('HalfWaveRectifier.png'); %% assigning axes1 to be the axes where to plot the photo. axes(handles.axes1) %% show the photo in the assigned axes. imshow(HalfWaveRectifier)
%% creates an axes which covers the whole GUI BG = axes('unit', 'normalized', 'position', [0 0 1 1]); %% importing the background photo. BackGround = imread('BuiltInBorad.png'); imagesc(BackGround); %% prevent plotting over the background and turn the axis off set(BG,'handlevisibility','off','visible','off') %% To makes the background behind everything. uistack(BG, 'bottom');
% Choose default command line output for About handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes About wait for user response (see UIRESUME) % uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line. Engineering Software & Applications 25
function varargout = About_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output;
% --- Executes on button press in HomeGUI. function HomeGUI_Callback(hObject, eventdata, handles) % hObject handle to HomeGUI (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
%% returns back the home GUI if this button is clicked HomeGUI;
% --- Executes on button press in D. function D_Callback(hObject, eventdata, handles) % hObject handle to D (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in VideoRectifier. function VideoRectifier_Callback(hObject, eventdata, handles) % hObject handle to VideoRectifier (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
%% opens the video GUI if the button is clicked Video;
function varargout = Video(varargin) % VIDEO MATLAB code for Video.fig % VIDEO, by itself, creates a new VIDEO or raises the existing % singleton*. % % H = VIDEO returns the handle to a new VIDEO or the handle to % the existing singleton*. % % VIDEO('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in VIDEO.M with the given input arguments. % % VIDEO('Property','Value',...) creates a new VIDEO or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before Video_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to Video_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help Video % Last Modified by GUIDE v2.5 27-Sep-2013 22:17:20 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @Video_OpeningFcn, ... 'gui_OutputFcn', @Video_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end
if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT
% --- Executes just before Video is made visible. function Video_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to Video (see VARARGIN)
%% it will search for the assigned [activex1] and shows the video in handles.activex1.URL = 'RectifierAnimation.wmv'; Engineering Software & Applications 27
%% creates an axes which covers the whole GUI BG = axes('unit', 'normalized', 'position', [0 0 1 1]); %% importing the background photo. BackGround = imread('BuiltInBorad.png'); imagesc(BackGround); %% prevent plotting over the background and turn the axis off set(BG,'handlevisibility','off','visible','off')
%% To makes the background behind everything. uistack(BG, 'bottom'); %% code source :(Shang W., 2012)
% Update handles structure guidata(hObject, handles); % UIWAIT makes Video wait for user response (see UIRESUME) % uiwait(handles.figure1); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %-----------------------------------------------------------%
% --- Outputs from this function are returned to the command line. function varargout = Video_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure % ~~~~~~% varargout{1} = handles.output;
% --- Executes on button press in Back. function Back_Callback(hObject, eventdata, handles) % hObject handle to Back (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
%% opens the about GUI if the button is clicked About;
Codes for Task 1 D [Main GUI] function varargout = D(varargin) % D MATLAB code for D.fig % D, by itself, creates a new D or raises the existing % singleton*. % % H = D returns the handle to a new D or the handle to % the existing singleton*. % % D('CALLBACK',hObject,eventData,handles,...) calls the local % function named CALLBACK in D.M with the given input arguments. % % D('Property','Value',...) creates a new D or raises the % existing singleton*. Starting from the left, property value pairs are % applied to the GUI before D_OpeningFcn gets called. An % unrecognized property name or invalid value makes property application % stop. All inputs are passed to D_OpeningFcn via varargin. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help D
% Last Modified by GUIDE v2.5 27-Sep-2013 23:30:21
% Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @D_OpeningFcn, ... 'gui_OutputFcn', @D_OutputFcn, ... 'gui_LayoutFcn', [] , ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end
if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT
% --- Executes just before D is made visible. function D_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin command line arguments to D (see VARARGIN)
Engineering Software & Applications 29
%% creates an axes which covers the whole GUI BG = axes('unit', 'normalized', 'position', [0 0 1 1]); %% importing the background photo. BackGround = imread('BuiltInBorad.png'); imagesc(BackGround); %% prevent plotting over the background and turn the axis off set(BG,'handlevisibility','off','visible','off') %% To makes the background behind everything. uistack(BG, 'bottom');
% Choose default command line output for D handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes D wait for user response (see UIRESUME) % uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line. function varargout = D_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output;
% --- Executes on slider movement. function CapacitorClider_Callback(hObject, eventdata, handles) % hObject handle to CapacitorClider (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'Value') returns position of slider % get(hObject,'Min') and get(hObject,'Max') to determine range of slider
%% assigns (value) variable to be equals to the sloder's value. value = get(handles.CapacitorClider, 'Value'); set(handles.CapacitorDisp, 'String', ['Value[uF] = ' num2str(value)])
% --- Executes during object creation, after setting all properties. function CapacitorClider_CreateFcn(hObject, eventdata, handles) % hObject handle to CapacitorClider (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: slider controls usually have a light gray background. if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor',[.9 .9 .9]); end Engineering Software & Applications 30
% --- Executes on selection change in FrequencyPopup. function FrequencyPopup_Callback(hObject, eventdata, handles) % hObject handle to FrequencyPopup (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns FrequencyPopup contents as cell array % contents{get(hObject,'Value')} returns selected item from FrequencyPopup
% --- Executes during object creation, after setting all properties. function FrequencyPopup_CreateFcn(hObject, eventdata, handles) % hObject handle to FrequencyPopup (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end
function ResistorValue_Callback(hObject, eventdata, handles) % hObject handle to ResistorValue (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of ResistorValue as text % str2double(get(hObject,'String')) returns contents of ResistorValue as a double
% --- Executes during object creation, after setting all properties. function ResistorValue_CreateFcn(hObject, eventdata, handles) % hObject handle to ResistorValue (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. Engineering Software & Applications 31
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end
function VoltageValue_Callback(hObject, eventdata, handles) % hObject handle to VoltageValue (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of VoltageValue as text % str2double(get(hObject,'String')) returns contents of VoltageValue as a double
% --- Executes during object creation, after setting all properties. function VoltageValue_CreateFcn(hObject, eventdata, handles) % hObject handle to VoltageValue (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end
%% Execute next codes when pressing this button function Plot_Callback(hObject, eventdata, handles) % hObject handle to Plot (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
%% gets the values of vo, R and checks if it is numerical or not Vo = str2num(get(handles.VoltageValue, 'String')); R = str2num(get(handles.ResistorValue, 'String')); X = isempty(Vo); Y = isempty(R); %% Assigning X,Y to be true, if the array of the input has any [empty{Non- numeric}] element. if X == 1 warndlg('Please Enter A Numerical Value for voltage!! : '); stop; elseif Vo < 10 || Vo > 20 warndlg('Please Enter a Voltage between [10-20]v '); stop; elseif Y == 1 Engineering Software & Applications 32
warndlg('Please Enter A Numerical Value for Resistance !! : '); stop; elseif R < 1500 || R > 2500 warndlg('Please Enter a Voltage between [1500-2500]v '); stop; end
%% checks the pop-up menu for the frequency and assigns the values as it was %% designed previously choice = get(handles.FrequencyPopup, 'Value'); switch choice case 1 F = 30; case 2 F = 60; case 3 F = 90; end
%% this code will read the written values in the capacitance displayer X = get(handles.CapacitorDisp, 'string'); X = str2num(X); Y = isempty(X); %% if the value of the capacitive displayer is numerical, it will take it %% and substiute for it. if Y == 0 c = get(handles.CapacitorDisp, 'string'); c = str2num(c); %% in the capacitance displayer has non-numerical values, the GUI will %% directly neglect it and read the value of capacitance slider and %% substitue it. elseif Y == 1 c = get(handles.CapacitorClider, 'Value'); end
%% to uF conversion. C = c/1000000; w = 2 * pi * F; t = 0 : 0.00001 : 0.07; T = length(t); state = 'Diode-On'; for i = 1 : T Vi(i) = Vo * sin(w * t(i)); switch state case 'Diode-On' Vr(i) = Vi(i); Ir = Vi(i)/R; Ic = w * C * Vo * cos(w * t(i)); A = Ir + Ic; if A <= 0 state = 'Diode-Off'; ta = t(i); end case 'Diode-Off' Vr(i) = Vo* sin(w * ta).* exp(-(t(i)-ta)./(R*C)); if Vi(i) >= Vr (i) state = 'Diode-On'; end end Engineering Software & Applications 33
end
%% sets axes1 to be the axes where it plots on. axes(handles.axes1) plot(t , Vi , 'r'); hold on plot(t , Vr , 'b--'); hold off xlabel(' Time (s) '); ylabel(' Voltage (V) '); legend (' Supply Voltage ', ' Resistors Voltage '); title(' Comparing the voltage supply to the resistors volage ');
function DefultHighMenu_Callback(hObject, eventdata, handles) % hObject handle to DefultHighMenu (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
%% if this defult button is pressed it will set voltage =20, R = 2500 , %% F= 90, Cslider= 50 and show C value in the Cdisplayer. set(handles.VoltageValue, 'String', num2str(20)) set(handles.ResistorValue, 'String', num2str(2500)) set(handles.CapacitorClider, 'Value', 50); set(handles.CapacitorDisp, 'String', 'Value[uF] = 50') set(handles.FrequencyPopup, 'Value', 3);
% -------------------------------------------------------------------- function uipushtool2_ClickedCallback(hObject, eventdata, handles) % hObject handle to uipushtool2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
%% this button sets the frequency to be 60Hz. set(handles.FrequencyPopup, 'Value', 2);
% -------------------------------------------------------------------- function DefultLowMenu_Callback(hObject, eventdata, handles) % hObject handle to DefultLowMenu (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA)
%% if this defult button is pressed it will set voltage =10, R = 1500 , %% F= 30, Cslider= 10 and show C value in the Cdisplayer. set(handles.VoltageValue, 'String', num2str(10) ) set(handles.ResistorValue, 'String', num2str(1500) ) set(handles.CapacitorClider, 'Value', 10 ); set(handles.CapacitorDisp, 'String', 'Value[uF] = 10') set(handles.FrequencyPopup, 'Value', 1 );