MATLAB Programming

1. Using command prompt:-

fx>> x=10

fx>> y=20

fx>> z=x+y

2. Matlab script (saved using .m extension)

x=10;

y=20;

z=x+y

3. Matlab program to accept two input from user and display its addition

a=input('Enter first number');

b=input('Enter Second number');

c=a+b

4. Matlab program to Find the Area of Circle by accepting radius from user

clc %This command will clear the command window

r=input('Enter the value for radius');

area =pi*r^2

5. Matlab program to Find the Area of Triangle by accepting base and height from user

clc

base=input('Enter the value for Triangle Base');

height =input('Enter the value for Triangle Height');

area=0.5*base*height

6. Program to calculate the plot the fuction sin(x) for 0<=x<=6

x=0:0.1:6;

y=sin(x);

plot(x,y);

6. % Enter the details of your name, college and course and age

clc

clear

name=input('Enter your name','s');

college =input('Enter your college name','s');

course=input('Enter your couse name','s');

age=input('Enter your couse name');

disp(name);

disp(college)

disp(course);

disp(age);

7. %find the sum of 20 integer number

clc;

clear;

sum=0;

for j=0:20

sum=sum+j;

end

fprintf("sum of 20 numbers =%d\n",sum);

8. %find the sum of integer number in vector

clc;

clear;

sum=0;

for j=[1 2 3 6 7]

sum=sum+j;

end

fprintf("sum of vector numbers =%d\n",sum);

9. %for loop in dicrement way

clc;

clear;

result=0;

for j=20:-2:0

result=result+j;

end

fprintf("Result =%d\n",result);

10. % Program to find the sum and average of 10 numbers

clc;

clear;

avg=0;

sum=0;

for i=1:10

n=input('Enter the number');

sum=sum+n;

end

fprintf("Sum =%d\n",sum);

fprintf("Average =%d\n",avg);

11. % Program to find find the count of even number in given n numbers

clc;

clear;

n=input('Enter Total Number');

for i=1:n,

num(i)=input('Enter the values');

end;

total=0;

for i=1:n,

if(rem(num(i),2)==0)

total=total+1;

end;

end;

fprintf("Total =%d\n",total);