Sie sind auf Seite 1von 16

LAB.NO.

Programs and their output with graphs


Q1. Given x=-5+9i and y=6-2i. Find x+y, xy, x/y?

Solution.
x=-5+9i;
y=6-2i;
z=x+y
t=x*y
u=x/y

The result of this code will be as follows.

z =1.0000 + 7.0000i

t = -12.0000 +64.0000i

u = -1.2000 + 1.1000i

Q2. Use matlab to compute 6(351/4)+140^0.35.

Solution.
type the following lines of code into the editor.

x=6*(351/4)+(140^0.35)
The output of this program is given bellow.
x =532.1383

Q3.Use MATLAB to plot the function


y=4(6x+1)^1/2 and z=5e^0.3x over the interval 0<-
x<-1.5?
Solution.

type the following lines of code into the editor.


x=0:0.5:1.5;
y=4*sqrt(6*x+1)
z=5*((exp(0.3*x))-2*x)
subplot(221)
plot(y)
subplot(222)
plot(z)

Q4. Use matlab to plot the function


s=2sine(3t+2)+(5t+1)^1/2 over the interval 0<-5<-.
Put a title on the plot, and properly label the axes.
The variable S represents speed in feet per
second, the variable t represents time in seconds.
Solution.
type the following lines of code into the editor.

t=0:0.5:5;
s=2*sin(3*t+2)+sqrt(5*t+1)
plot(t,s)
title('Speed');
xlabel('time');
ylabel('speed');
LAB No 2

Q.No.1:-Plot the following functions by using sub


plot camand.

1- e^-t,
2- e^-2t.
3- -e^-2t.
&
also plot the functions

(e^-t - e^-2t)
Solution.
Code for the above program is as follows.
syms x y z t
t=0:.05:5;
x=exp(-t);
plot(x)
grid on
xlabel('t')
ylabel('Amp')
title('exp(-t)')
subplot(2,2,1)

y=exp(-2*t)
plot(y)
grid on
xlabel('t')
ylabel('Amp')
title('exp(-2*t)')
subplot(2,2,2),

z=-exp(-2*t)
plot(z),
grid on
xlabel('t')
ylabel('Amp')
title('-exp(-2*t)')
subplot(2,2,3)

q=exp(-t);
w=-exp(-2*t);
e=q+w;
plot(e)
grid on
xlabel('t')
ylabel('Amp')
title('exp(-t)-exp(-2*t)')
subplot(2,2,4)
LAB No:3

Q1. Write a matlab script to plot the function


T=6ln(t)-7^0.2*t over the interval 1<t<3. Put a title
on the plot and properly label the axes. The
variable T represents temperature in degree
Celsius, the variable t represents time in minutes.
Solution:
type the following lines of code into the editor.

x = 1:0.1:3;
T = 6*log(t)-7*(exp .2*t);
plot (x,T);
title(‘Temperature in celcius’);
ylabel(‘Y axis’’);
xlabel(‘x axis’’);

Select the ‘File -> Save’ menu of the editor, and save the file by
the name of‘firstprog’. Now, close the Editor.
LAB No.4

Q.No.1:-Write an M-file which takes a no. as input


and calculates its factorial.
Solution.
The code for this program is given bellow.
x=input('please enter your nukmber')
factorial(x)
in this program the input is ‘6’ and its factorial is 720.
the code and its result is given bellow.
Q2. Write a program in which you take the grade
as an input and calculates the cgpa.
Solution.

x=input('please enter the grade of Ist sub')


y=input('please enter the grade of 2nd sub')
z=input('please enter the grade of 3rd sub')
g=(x+y+z)/3
display('GPA')
disp(g)
LAB.No.5
Q.No.1:- . Use a for loop to plot the following
function over the interval -2 ≤ x ≤ 6.
Properly label the plot. The variable y represents
height in kilometers, and the
variable x represents time in seconds.

Solution.
X=[-2:0.1:6];
For k =1:length(x)
If x(k) <-1
Y(k) = exp(x(k)+10;
elseif x(k)=2+cos(pi*x(k)<5
Y(k)=10*(x(k));
else y(k)=10*10(x(k)-5)+1;
end
end
plot(x,y);
xlabel(‘x-time in seconds’)
ylabel(‘y-hight in kello meters’)
Q.No.2: Write a script file to determine how many
terms are required for the sum
of the series 5k2 - 2k, k =1,2,3,….. to exceed
10,000. What is the sum for this many
terms?
Solution:
Because we do not know how many times we must evaluate the
expression
5k2 – 2k, we use a while loop. The script file is the following:
clc;
clear;
total = 0;
k = 0;
while total < 1e+4
k=k+1;
total = 5*k^2 – 2*k + total;
end
disp(‘The number of terms is:’)
disp(k)
disp(‘The sum is: ‘)
disp(total)

Q.No3: Determine how long it will take to


accumulate at least Rs.10,000 in a bank
account if you deposit Rs.500 initially and Rs.500
at the end of each year, if the
account pays 5 percent annual interest.
Solution:
Because we do not know how many years it will take, a while
loop should
be used. The script file is the following:
amount = 500;
k = 0;
while amount < 10000
k=k+1;
amount = amount*1.05 + 500;
end
amount
k
The final results are amount = 1.0789e+004, or Rs.10,789, and
k=14, or 14 years!

Das könnte Ihnen auch gefallen