Saturday, February 22, 2014

Image Processing Example in Matlab!


Example: Write Matlab script to arbitrary Clip a segment (sub image) from a given source image as illustrated in the two figures below, and compare image sizes before and after. This procedure is to be repeated as long as user needs:
 Solution:
%===========================================
%= Program Script to arbitrary selecting Sub image =
%= From a source image by defining area to clip!    =
%===========================================

key=1;
while key ==1
    clc
    clear all
    IS=imread('MechaTron.jpg');
    figure;
    imshow(IS);
    input ('Enter x1: ');
    x1=ans;
    input ('Enter x2: ');
    x2=ans;
    input ('Enter y1: ');
    y1=ans;
    input ('Enter y2: ');
    y2=ans;
    Iss=IS(y1:y2,x1:x2,1:3);
    figure;
    imshow(Iss);
    s1=size(IS);
    s2=size(Iss);
    display ('==============================')
    display ('Size of Original image '); s1
    display ('Size of Sub image '); s2
    input ('Hit any Return key to End or 1 to Continue!');
    key =ans;
    close all
end
close all

Friday, February 14, 2014

Unsolved Matlab Excercise 4 students!

Exercise: Write a Matlab program script to plot the following functions in One figure:
y = exp (-x2), y = sin (3x) and y = log (1.5π.x)
for x values (- 2.5 : 0.02 : 2.5) , that yields the following figure:

Matlab Program to Plot Multi Curves in One Figure with Legends!

Example: Write the Matlab script to Plot 2 Curves in the same figure for the functions: Y = exp (X2), and Y = sin (X).



Solution:
%=============================================
%=Plotting 2 Graphs at the same figure with Legends!=
%=============================================

X=[-1:0.01:1];
Y=exp(X.^2);
plot(X,Y)
hold on
X=[-1:0.01:1];
Y=sin(X);
plot(X,Y,'r')
xlabel('(X) values')
ylabel('(Y) values')
title('Functions y = exp (X^2) and y = sin (x) ')
legend('exp (x^2)','sin (x)',2);
%**************************************************************
Another Example:  Write Matlab program script to plot 5 curves using x column vector values 1:1000 for the function y = Log(x)?















Solution:
%======================================================
%= Plotting Multiple curves of a function using a vector elements=
%======================================================

x=(1:1000)'; % Transpose of a row vector!
% Or, we can write it: x = [1:1000;];
for k =1:5
    y(:,k)=k*log10(x);
end
plot(x,y)
title('Multi Curves Plot!');
xlabel('x vector values');
ylabel('y = log (x)')
legend('log(x)','2log(x)','3log(x)','4log(x)','5log(x)');

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: