Saturday, August 11, 2018

Monte Carlo Crude Integration (User Defined Function)

Monte_clarlo_integration
% _________________________________________________________________________
% Monte Carlo crude integration
% By Mahesha MG, MIT
% Date: 11/08/2018
% _________________________________INPUT___________________________________
clc;
clear;
f=input('Enter a valid MATLAB expression in x : ','s');
a=input('Enter lower limit: ');
b=input('Enter upper limit: ');
n=input('Enter sampling number: ');
x=a+(b-a)*rand(1,n);
s=sum(eval(f));
result=(b-a)*s/n
% ______________________________SAMPLE OUTPUT______________________________
% Enter a valid MATLAB expression in x : x.^2+x
% Enter lower limit: 2
% Enter upper limit: 5
% Enter sampling number: 10000
%
% result =
%
%    49.3144
% _________________________________________________________________________

Friday, June 10, 2016

Optical Ray Tracing

ray_tracing
%==========================================================================
% Title: Ray tracing by matrix technique
% Author: Mahesha MG, Manipal Intitute of Technology Manipal University
% Date: 10/08/2015
%==========================================================================
% Variables
% r0 --> initial height from optic axis
% theta0 --> initial angle with normal
% M --> Propagation/Refraction matrix
%==========================================================================
clear;
clc;
D=zeros(1,20);
R=zeros(1,20);
r0 = input('Enter initial height from optic axis (in m): ');
theta0 = input('Enter initial angle with normal (in radian): ');
R0=[r0;theta0];
D=0;
td=0;
R=r0;
while(1)
    display('__________________OPTIONS__________________');
    display('0 --> Exit');
    display('1 --> Propagation');
    display('2 --> Refraction');
    display('3 --> Thin lens');
    display('4 --> Reflection');
    display('5 --> Spherical Mirror');
    display('____________________________________________');
    ch=input('Enter option: ');
    if ch == 0
    break;
    else if ch == 1
        d=input('Enter distance (in m): ');
        M=[1 d; 0 1];   %Propagation matrix
        else if ch == 2
                n0=input('Refractive index of first medium: ');
                n1=input('Refractive index of second medium: ');
                d=0;
                M=[1 0; 0 n0/n1];  %Refraction matrix
            else if ch == 3
                f=input('Focal length of the lens: ');
                d=0;
                M=[1 0; -1/f 1];   %Thin lens matrix
                else if ch == 4
                        d=0;
                        M=[1 0; 0 -1]; %Reflection matrix
                    else if ch == 5
                            d=0;
                            R=input('Enter radius of curvature of spherical mirror: ');
                            M=[1 0; -2/R -1];
        else warning('Entered option is invalid');
                        end
                    end
                end
            end
        end
    end
  R1=M*R0;
  td=td+d;
  D=[D;td];
  R=[R;R1(1)];
  R0=R1;
  end
figure('color','white');
plot(D,R,'b-+',D,-R,'r-+');
%==========================SAMPLE INPUT====================================
% Enter initial height from optic axis (in m): 0.02
% Enter initial angle with normal (in radian): 0.01
% __________________OPTIONS__________________
% 0 --> Exit
% 1 --> Propagation
% 2 --> Refraction
% 3 --> Thin lens
% 4 --> Reflection
% 5 --> Spherical Mirror
% ____________________________________________
% Enter option: 1
% Enter distance (in m): 0.1
% __________________OPTIONS__________________
% 0 --> Exit
% 1 --> Propagation
% 2 --> Refraction
% 3 --> Thin lens
% 4 --> Reflection
% 5 --> Spherical Mirror
% ____________________________________________
% Enter option: 3
% Focal length of the lens: 0.06
% __________________OPTIONS__________________
% 0 --> Exit
% 1 --> Propagation
% 2 --> Refraction
% 3 --> Thin lens
% 4 --> Reflection
% 5 --> Spherical Mirror
% ____________________________________________
% Enter option: 1
% Enter distance (in m): 0.1
% __________________OPTIONS__________________
% 0 --> Exit
% 1 --> Propagation
% 2 --> Refraction
% 3 --> Thin lens
% 4 --> Reflection
% 5 --> Spherical Mirror
% ____________________________________________
% Enter option: 0
% =========================================================================