Monday, February 03, 2014

If, For, and While statements in Matlab...with a practicle example for Mechatronics & Refrigeration-A/C 2nd.year students!


This is a simple Matlab Program script about If, For, and While statements.
In this example we will solve the problem of computing an Electricity formula Called Total Harmonic Distortion  for any given up to 10 Harmonic Voltages with a Condition that if No. of these harmonic voltages =0 then program ends !
This problem is as illustrated below:
Solution:
%========================================================
%=This Program Tests the While, for, and If statements in Matlab =
%========================================================
clc
display 'Calculating the Total Harmonic Distortion!'
display 'Using the formula: THD = sqrt(V2^2+V3^2+...+Vn^2)/V1'
display '==================================================='
ans=-1; THD=0;
input 'Enter Fundamental Voltage: '
v(1)=ans;
while ans ~= 0
    input 'Please Enter a Number of Harmonic Voltages(2-10), or Zero to Exit: ';
    n=ans;
    if n~=0
       for i=2:n+1
          input 'Enter Harmonic Voltage: '
          v(i)=ans;
          THD = (v(i)^2)+THD;
       end        % end of for statement
    display 'Total Harmonic Voltage is: '; sqrt(THD)/v(1)
    display '================================================'
    else display 'End of Program !'; end          % end of if statement
end            % end of while statement
%--------------------------------------------------------------------------------
The Run of the programs will display the following on Command window:

0 Comments:

Post a Comment

السلام عليكم...سارد بعد قرائتي للتعليق...شكرا

<< Home