Color to Grayscale Conversion: MATLAB Image Processing Tutorial

Hi friends, this is my first tutorial on Matlab image processing.The purpose of this tutorial is to gain familiarity with MATLAB’s Image Processing Toolbox. Let us start the basic of the image processing with a very simple example i.e. converting a color image of any format (ex. jpeg, jpg, bmp, etc) into a grayscale image.

MATLAB Program for Color to Grayscale Conversion

Step 1:

Initially, you have to define a matrix A with a path of the image that you have to convert. For this purpose, if you can’t copy a path of the image file, just install this small software (http://www.novell.com/coolsolutions/tools/downloads/pathc400.zip) after installing this software simply right click on the image you will see an option for copy long URL. OK after copying the image path.

Step 2:

Define matrix B with the image format. In this case jpg. You can change the format you have.

Step 3:

C=rgb2gray(B).   Here rgb stands for RedGreenBlue i.e. color image gray stands for converting it into grayscale.

%tutorial M-file
%created by: myclassbook.org
%created on: 20 may 2013

% Load a color .jpg image and convert it into grayscale

A=’H:New photoImagesPhoto1619.jpg’; %designate matrix A as the specified file
B=imread(A,’jpg’)                            %matrix B loaded with jpg file specified by A
figure (1),imshow(B);                     %show image B in figure window 1
C=rgb2gray(B)                                 %convert color image to grayscale image
figure (2),imshow(C);                    %show image C in figure window 2

MATLAB Image Processing
MATLAB Image Processing

MATLAB Image Processing
MATLAB Image Processing

MATLAB Image Processing
MATLAB Image Processing

If you like this article, please share it with your friends and like or facebook page for future updates. Subscribe to our newsletter to get notifications about our updates via email. If you have any queries, feel free to ask in the comments section below. Have a nice day!

Newton’s Forward Interpolation Formula with MATLAB Program

In everyday life, sometimes we may require finding some unknown value with the given set of observations. For example, the data available for the premium, payable for a policy of Rs.1000 at age x, is for every fifth year. Suppose, the data given is for the ages 30, 35, 40, 45, 50 and we are required to find the value of the premium at the age of 42 years, which is not directly given in the table. Here we use the method of estimating an unknown value within the range with the help of given set of observation which is known as interpolation.

Definition of Interpolation

Given the set of tabular values (x0, y0), (x1, y1),…,(xn, yn) satisfying the relation y=f(x) where the explicit nature of f(x) is not known, it is required to find a simpler function say ?(x), such that f(x) and ?(x) agree at the set of tabulated points. Such a process is called as interpolation.

If we know ‘n’ values of a function, we can get a polynomial of degree (n-1) whose graph passes through the corresponding points. Such a polynomial is used to estimate the values of the function at the values of x.

We will study two different interpolation formula based on finite differences when the values of x are equally spaced. The first formula is:

Newton’s forward difference interpolation formula:

The formula is stated as:

Newton’s forward difference interpolation formula

Where ‘a+ph’ is the value for which the value of the function f(x) is to be estimated. Here ‘a’ is the initial value of x and ‘h’ is the interval of differencing.

Question

The table gives the distance between nautical miles of the visible horizon for the given height in feet above the earth surface.  Find the value of y when x= 218 feet.

Newtons Forward Interpolation Formula
Newton’s Forward Interpolation Formula

MATLAB Program for Newtons Forward Interpolation Formula

Newtons Forward Interpolation Formula
Newton’s Forward Interpolation Formula

Newtons Forward Interpolation Formula
Newton’s Forward Interpolation Formula

You may also like

If you like this article, please share it with your friends and like or facebook page for future updates. Subscribe to our newsletter to get notifications about our updates via email. If you have any queries, feel free to ask in the comments section below. Have a nice day!

Newton Raphson Method & It’s MATLAB Program

Introduction to Iterative methods: There are number of iterative methods like  Jacobi method, Gauss–Seidel method that has been tried and used successfully in various problem situations. All these methods typically generate a sequence of estimates of the solution which is expected to converge to the true solution. Newton-Raphson method is also one of the iterative methods which are used to find the roots of given expression.

Newton-Raphson Method with MATLAB code:

If point x0 is close to the root a, then a tangent line to the graph of f(x) at x0 is a good approximation the f(x) near a. So the root of the tangent line, where the line cuts the X-axis; x1 is the better approximation to a than x0 is.

Slope of the tangent =

1

Therefore

2

Repeating process, we obtain a better approximation,3

Continue in this way. If xn is the current estimate, then the next estimate xn+1 is given by

4

if x0 is sufficiently close to a, xn?a as n?8.

Limitations of Newton-Raphson Method

  1. If initial guess is too far away from the required root, the process may converge to some other root.
  2. Division by zero may occur if f’(xi) is zero or very close to zero.
  3. A particular value in the iteration sequence may repeat, resulting in an infinite loop.

Newton-Raphson MATLAB program:

% Newton Raphson Method
 clear all
 close all
 clc
 % Change here for different functions
 [email protected](x) cos(x)-3*x+1
 %this is the derivative of the above function
 [email protected](x) -sin(x)-3
 % Change lower limit 'a' and upper limit 'b'
 a=0; b=1;
 x=a;
 for i=1:1:100
 x1=x-(f(x)/df(x));
 x=x1;
 end
 sol=x;
 fprintf('Approximate Root is %.15f',sol)
 a=0;b=1;
 x=a;
 er(5)=0;
 for i=1:1:5
 x1=x-(f(x)/df(x));
 x=x1;
 er(i)=x1-sol;
 end
 plot(er)
 xlabel('Number of iterations')
 ylabel('Error')
 title('Error Vs. Number of iterations')

ANSWER :
f =
@(x)cos(x)-3*x+1
df =
@(x)-sin(x)-3
Approximate Root is 0.6071016481031231

Newton Raphson Method MATLAB program
Newton-Raphson Method MATLAB program

You may also like

If you like this article, please share it with your friends and like or facebook page for future updates. Subscribe to our newsletter to get notifications about our updates via email. If you have any queries, feel free to ask in the comments section below. Have a nice day!

Gauss Seidel Method MATLAB Program & Algorithm

We have studied in the last article that, the preceding methods of solving simultaneous linear equations are known as direct methods as they yield the exact solution. On the other hand, an iterative method is that in which we start from an approximation to the true solution and obtain better and better approximation from a computation cycle repeated as often as may be necessary for achieving the desired accuracy. Simple iteration methods can be devised for systems in which the coefficient of leading diagonal is large compared to others.

In the last article about solving roots of given simultaneous equations, we have studied Jacobi’s iterative method. Similarly, there is another method for solving roots of simultaneous equations which is called as Gauss-Seidel Iterative Method. After that, we will see MATLAB program on how to find roots of simultaneous equations using Gauss-Seidel Method.

Let us consider set of simultaneous equations as follows:

  • a1x+b1y+c1z=d1…………………1)
  • a2x+b2y+c2z=d2…………………1)
  • a3x+b3y+c3z=d3…………………1)

We then solve above equation for x, y and z respectively. Then the system can be written in the form:

  • x = (k1) – (l1)y – (m1)z…………..2)
  • y = (k2) – (l2)x – (m2)z…………….2)
  • z = (k3) – (l3)x – (m3)y…………….2)

Gauss-Seidel Method is a modification of Jacobi’s iteration method as before we starts with initial approximations, i.e. x0=y0=z0=0 for x, y and z respectively.

Substituting y=y0, z=z0 in the equation x1=k1, then putting x=x1, z=z0 in the second of equation (2) i.e.

  • (y1) = (k2) – (l2)x1 – (m2)z0

Substituting x=x1, y=y1 in the third of equation (2) i.e.

  • (z1) = (k3) – (l3)x1 – (m3)y1  and so on.

As soon as a new approximation for an unknown is found it is immediately used in the next step. This process is then repeated till the desired degree of accuracy is obtained.

Let us discuss Gauss-Seidel method by solving one simple example.

Example

Find the roots of following simultaneous equations using the Gauss-Seidel method.

20x+y-2z=17…………..1)

3x+20y-z=-18…………..1)

2x-3y+20z=25…………..1)

Solution

Let us write the given equation in the form as:

x = (1/20)(17-y+2z)……………..2)

y = (1/20)(-18-3x+z)…………….2)

z = (1/20)(25-2x+3y)……………2)

we start approximation by x0=y0=z0=0.

Substituting y=y0 and z=z0 in right hand side of first of equation (2)

X1 = 17/20=0.85

In second of equation second put x=x1 and z=z0

Y1 = (1/20)(-18-3*0.85) = -1.0275

Put x=x1, y=y1 in third of equation (2)

Z1 = (1/20)(25-2*0.85+3*-1.0275) = 1.011

Similarly we get,

x2=1.002; y2 =-0.9998; z2 =0.9998

x3 =1.0000; y3 =-1.0000; z3 =1.0000

x4 =1.0000; y4 =-1.0000; z4 =1.0000

Now it is sufficient. If you observe above two sets of roots, they are almost same.

Hence the roots of given simultaneous equations using Gauss-Seidel Method are:

x=1;  y = -1;  z=1

MATLAB program for Gauss-Seidel Method

MATLAB code for Gauss Seidel method
MATLAB code for Gauss-Seidel method

If you like this article, please share it with your friends and like or facebook page for future updates. Subscribe to our newsletter to get notifications about our updates via email. If you have any queries, feel free to ask in the comments section below. Have a nice day!

False Position (Regula falsi) Method Matlab Program, Algorithm & Flowchart

The false position method or regula falsi method is a term for problem-solving methods in arithmetic, algebra, and calculus. In simple terms, these methods begin by attempting to evaluate a problem using test (“false”) values for the variables, and then adjust the values accordingly.

False Position method is the oldest method for finding the real roots of an equation f(x)=0. Also, this method closely resembles with Bisection method.

In False Position method, we choose two points x0 and x1, such that f(x0) and f(x1) are of opposite sign. So the abscissa of point where the chords cuts the x-axis (y=0) is given by,

1

This is the first approximation to the root.

If now f(x0) and f(x2) are of opposite sign then the root lies between x0 and x2, so by replacing x1 by x2.

By using the following equation we obtain the second approximation:

2

Similarly, we can obtain next approximations using same equations as above. After certain approximations (when two consecutive approximations are almost same) we will get our final answer. At the end of this article, we will see how to write MATLAB program for the False Position method.

Let us solve one simple problem for clear understanding.

Example

Find the real root of the equation  x^3-2x-5=0 by using false position method.

Solution

Let f(x) = x^3-2x-5

  • F(2) = -1 = negative
  • F(3) = 16 = positive

Hence the roots of given equation lies between (2,3)

Let us find first approximation (x2) by taking x0=2, x1=3,

X2 = x0 – {(x1 – x0)/[f(x1) – f(x0)]}*f(x0)

Therefore, x2 = 2 – [(3-2)/(16+1)]*(-1) = 2.0588

Here, f(x2) = f(2.0588) = -0.3908 = negative

 

Hence, roots of given equation lies between (2.0588,3)

X3 = x1 – {(x2 – x1)/[f(x2) – f(x1)]}*f(x1)

Therefore, x3 = 2.0588 – [(3-2.0588)/(16+0.3908)]*(-0.3908) = 2.0813

Here, f(x3) = f(2.0813) = -0.1469 = negative

 

Hence roots lies between (2.0813,3).

Therefore, x4=2.0896

Here, f(x4)=-0.0547

 

Hence root lies between (2.0896,3)

Therefore, x5=2.0927

Here, f(x5)=-0.0202

 

Hence root lies between (2.0927,3)

Therefore, x6=2.0939

Here, f(x6)=-0.0075

 

Hence root lies between (2.0939,3)

Therefore, x7=2.0943

Here, f(x8)=-0.0027

 

Hence root lies between (2.0943,3)

Therefore, x9=2.0945

Here, f(x9)=-0.0010

 

Hence root lies between (2.0945,3)

Therefore, x10=2.0945

Now, x9 and x10 are equal, therefore we can stop here.

So our final answer is x=2.0945.

MATLAB program for False Position Method

False Position Method MATLAB Program
False Position Method MATLAB Program

Plot of Error of False Position Method
Plot of Error of False Position Method

Download

Click here to download the MATLAB program for false position method.

You may also like

If you like this article, please share it with your friends and like or facebook page for future updates. Subscribe to our newsletter to get notifications about our updates via email. If you have any queries, feel free to ask in the comments section below. Have a nice day!

Bisection Method Matlab Program With Flowchart & Algorithm

There are various methods available for finding the roots of given equation such as Bisection method, False position method, Newton-Raphson method, etc. Today I am going to explain Bisection method for finding the roots of given equation. I will also explain MATLAB program for Bisection method.

Bisection method is very simple but time-consuming method. In this method, we first define an interval in which our solution of the equation lies. As the name indicates, Bisection method uses the bisecting (divide the range by 2) principle. In this method, we minimize the range of solution by dividing it by integer 2.

Following are the steps to find the approximate solution of given equation using Bisection method:

Let us assume that we have to find out the roots of f(x), whose solution lies in the range (a,b), which we have to determine. The only condition for bisection method is that f(a) and f(b) should have opposite signs (f(a) negative and f(b) positive). When f(a) and f(b) are of opposite signs at least one real root between ‘a’ and ‘b’ should exist.

For the first approximation, we assume that root to be,

x0=(a+b)/2

Then we have to find a sign of f(x0).
If f(x0) is negative the root lies between a and x0. If f(x0) is positive the root lies between x0 and b.
Now we have new minimized range, in which our root lies.
The next approximation is given by,

  • x1 = (a+x0)/2………….if f(x0) is negative
  • x1 = (x0+b)/2………….if f(x0) is positive

In this taking midpoint of the range of approximate roots, finally, both values of range converge to a single value, which we can take as an approximate root.

Bisection Method Example:

Find the root of x^3 – x = 1 by using Bisection Method.

Solution:

Let us assume that the root of x^3 – x – 1=0 lies between (1,2)

Here, f(1) = negative and f(2) = positive.

Hence root lies between (1,2)

For first approximation,

x0 = (1+2)/2 = 1.5
f(x0) = f(1.5) = positive
Hence root lies between (1,1.5)

x1 = (1+1.5)/2 = 1.25
f(x1) = f(1.25) = negative
Hence root lies between (1.25,1.5)

x2 = (1.25+1.5)/2 = 1.375
f(x2) = f(1.375) = positive
Hence root lies between (1.25,1.375)

x3 = (1.25+1.375)/2 = 1.3125
f(x3) = f(1.3125) = negative
Hence root lies between (1.3125,1.375)

x4 = (1.3125+1.375)/2 = 1.34375
f(x4) = f(1.34375) = positive
Hence root lies between (1.3125,1.34375)

x5 = (1.3125+1.34375)/2 = 1.328125
f(x5) = f(1.328125) = positive
Hence root lies between (1.3125,1.328125)

x6 = (1.3125+1.328125)/2 = 1.320313
f(x6) = f(1.320313) = negative
Hence root lies between (1.320313,1.328125)

x7 = (1.320313+1.328125)/2 = 1.324219
f(x7) = f(1.324219) = negative
Hence root lies between (1.324219,1.328125)

x8 = (1.324219+1.328125)/2 = 1.326172
f(x8) = f(1.326172) = positive
Hence root lies between (1.324219,1.326172)

x9 = (1.324219+1.326172)/2 = 1.325195
f(x9) = f(1.325195) = positive
Hence root lies between (1.324219,1.325195)

x10 = (1.324219+1.325195)/2 = 1.324707
f(x10) = f(1.324707) = negative
Hence root lies between (1.324707,1.325195)

x11 = (1.324707+1.325195)/2 = 1.324951
f(x11) = f(1.324951) = positive
Hence root lies between (1.324707,1.324951)

x12 = (1.324707+1.324951)/2 = 1.324829
f(x12) = f(1.324829) = positive
Hence root lies between (1.324707,1.324829)

x13 = (1.324707+1.324829)/2 = 1.324768
f(x13) = f(1.324768) = positive
Hence root lies between (1.324707,1.324768)

OK stop it …. I know you are getting tired… but there is no shortcut.

Now if you observe two limits (1.324707, 1.324768) of above range, they are almost same, which is our root of given equation.

Answer: x=1.3247

Algorithm for Bisection Method:

  1. Input function and limits.
  2. Repeat steps 3 and 4 100 times.
  3. x=(a+b)/2
  4. If f(x0)<0, a=x  else b=x
  5. Display x
  6. Repeat steps 7 and 8 10 times.
  7. Error = x-(a+b)/2
  8. Store error values in array
  9. Plot error
  10. STOP.

I would recommend watching below video for complete understanding for Bisection method algorithm.

MATLAB program for bisection method:

% Crated by mayclassbook.org auther: mayuresh
% Created on 12 june 2013
% Bisection method
% Find the roots of x^3-x-1 using bisection method and plot the error
[email protected](x) x^3-x-1;
a=1;
b=2;
for i=1:100
  c=(a+b)/2;
  if f(c)>0
   b=c;
  else a=c;
  end
 end
 a=1; b=2; p=c;
 for i=1:100
  c=(a+b)/2;
 er(i)=f(c)-f(p);
 if f(c)>0
 b=c;
 else a=c;
 end
 end
 fprintf('Root of given equation is %f',c)
 plot(er);
 title('Plot of error')
 xlabel('Number of iterations')
 ylabel('Error')
 grid on;

Bisection method MATLAB program
Bisection method MATLAB program

The plot for error between interval is given by,

Bisection method plot of error

I would recommend watching below video for complete understanding for Bisection method example

You may also like

If you like this article, please share it with your friends and like our facebook page for future updates. Subscribe to our newsletter to get notifications about our updates via email. If you have any queries, feel free to ask in the comments section below. Have a nice day!