You are on page 1of 2

Department of EEE Experiment No.

1 Name of the Experiment: Introduction to Numerical Analysis and MATLAB In the C-language programming course, we learnt how to solve problems using C. Its difficult to solve mathematical problems due to variable type and some other reasons. We will learn MATLAB in order to solve different problems easily. Here is the conversion between syntax of C and MATLAB. Uses of ; C- language It is essential to declare a command int i; float f; double d; long l; scanf(%d, &i); MATLAB It is not essential. Without ; the program will print the corresponding value. Not necessary (and essentially not possible)to define variable type. Just, x=5; x=input(); It is possible to give some string to show while taking input, like, x=input(give one number); disp(x); But usually as not mentioning ; gives the mathematical output, so in most of the cases disp function is not used. For only string output, disp is used. if(condition) statement; else statement; end

Variable type

input from user

output to user

printf(%d, i);

if-else syntax

for loop syntax

if(condition) { statement; } else { statement; } for(initialization; condition checking; change of value) { statement; }

++ syntax

i++; means that i=i+1; i--; means that i=i-1;

for i=0:2:200 x=x+1; end means for variable=initial_value: increment: final_value statement; end no such syntax

array

undefined size of array

int a[2][2]; a[0][0]=1; a[0][1]=2; a[1][0]=3; a[1][1]=4; note that, in C, array index starts at 0 and ends at (n-1). It is not possible to declare array of undefined size, like, scanf(%d,&n); int a[2][n]; it is not possible in C // is used for single line comment /* is used for muti-line comment*/

a=zeros(2,2); a(1,1)=1; a(1,2)=2; a(2,1)=3; a(2,1)=4; In MATLAB, it starts at 1 and ends at n x=input(the size of array =); a=zeros(2,n); it is possible in MATLAB

comment

% is used for comment

Now, look at the code for determine the square root of a value greater than 1 in MATLAB, clc; clear all; % clears the screen % clears all previous variable

x = input(give a number greater than 1 ); for i=1: 0.0001: x if( i*i >= x) i break; end end

You might also like