Monday, February 11, 2013

Changing MATLAB windows color

Feeling to be colorful...

You can change the MATLAB window color. 

Goto File --> Preferences --> Colors(on the left side) --> Un-check "Use system color"

And change as you wanted...
Try the following. 

1. Terminal



2. Ubuntu


3. Cool


4. Bright



Try your own colors and let me know...



Sunday, February 10, 2013

Newton Raphson Method

Hello everybody...
The following is a sample program to understand finding solution of a non linear equation using Newton Raphson Method. This program is not a generalised one. But you can understand the basic idea of the method and how to implement it using MATLAB. For more information about this method please try this

% This programme is to find the solution of a non linear equation by Newton Raphson method.
% This is not a generalized program
clc;
clear all;
format long;
syms x;
e = 1e-5;                      % setting the tolerance value
dx = e + 1;
f = log(2-x) + x^2;               % enter your function here;
x = 7;                          % initially assumed value of x
count = 0;                      % setting counter to know the no of iterations taken
p = zeros(1,1);
while (abs(dx) > e)             % initialising the iteration and continue until the error is less than tolerance
    dx = eval(f/(diff(f)));     % calculating dx, diff is used for finding the differentiation of the fuction
    x = x - dx                 % updating the value of x
    count = count + 1;          % incrimenting the counter
    p(count) = x;
    drawnow();
    plot(abs(p),'r','linewidth',3);
    grid;
    if (count > 300)
        fprintf('Error...! Solution not converging !!! \n');  % printing the error message
        break;
    end
end
% plot(abs(p));
if (count < 300)
    fprintf('The solution = ');  %printing the result
    x
    fprintf('\nNumber of iteration taken = %d\n',count);
end

Try it for different functions
Thank you...

Wednesday, February 6, 2013

Convolution of two functions

Enter the following code in to your MATLAB editor. You could see how the convolution of two function in calculated. Try the same for your functions.


clc;
clear all;
t = 0  : 0.05 : 4;
x = inline ('(exp(-0.001*t)).*(t >= 0)','t');
h = inline ('(exp(-0.001*t).*(t >= 0)) + (exp(-3*t)).*(t>=0)','t');
dtau = 0.005;
tau = -1:dtau:4;
ti = 0;
tvec = -0.25:0.1:3.75;
y = NaN * zeros (1, length (tvec));
figure(1);
for t= tvec
    ti = ti + 1;
    xh = x(t - tau) .* h(tau);
    y(ti) = sum (xh .* dtau);
    subplot(2,1,1), plot(tau, h(tau), 'k-', tau,x(t-tau),'k--',t,0,'ok');
    axis([tau(1) tau(end) -0.25 3]);
    subplot(2,1,2), plot(tvec, y, 'k', tvec(ti), y(ti),'ok');
    axis([tau(1) tau(end) -0.25 5]);
    drawnow;
    pause(0.1);
end

Using inline function to plot discrete functions

Enter the following code in the matlab command window....
n = -3.5 : 0.1 : 3.5;
f = inline('(-1.5*n).*((n>=-3)&(n<-1)) + ((n>=-1)&(n<1)) + (.5*n+1).*((n>=1)&(n<=3))', 'n');
stem(f(n))



Now try each of the following:
stem(n,f(n),'*-')
stem(n,f(n),'--')

Monday, February 4, 2013

Using Inline fuction

inline(expression, arg1, arg2 ...) constructs an inline function whose input arguments are specified by the stringsarg1, arg2, ...  Multicharacter symbol names may be used. This you can get from the matlab help.

Now.... how we can use them. I will divide it into different cases


  1. Function with one variable.
It can be used as
t = -4 : 0.01 : 4;
f = inline('(exp(t)+1)', 't');
This will define your function. Now if you want to plot it in the whole range of t, use
plot(t, f(t));
This will give you the following output (* click on the image to zoom in)
If you want to make the graph thicker use
plot(t,f(t),'linewidth', 2);
Test it with different values in place of 2. 

2. Plotting different functions on same base

If you want plot different function for different input values then try as follows
f = inline('(-1.5*t+1) .* ((t>-3)&(t<0)) + (3) .* (t == 0)+ (1.5*t+1) .* ((t>0)&(t<3))');
plot(t,f(t),'r','linewidth', 3);
grid on;
You will get the