You are on page 1of 9

Task # 01 :- (task # 04 “according to slide”)

Explore the use of the functions round, ceil, floor and fix for the values x =
0.3, x = 1/3, x = 0.5, x = 1/2, x = 1.65 and x = −1.34.
Matlab Solution :-
For round:-
>> round x=0.3
ans = 120 61 48 46 51
>> round x=1/3
ans = 120 61 49 47 51
>> round x=0.5
ans = 120 61 48 46 53
>> round x=1/2
ans = 120 61 49 47 50
>> round x=1.65
ans = 120 61 49 46 54 53
>> round x=-1.34
ans = 120 61 45 49 46 51 52

For ceil :-
>> ceil x=0.3
ans = 120 61 48 46 51
>> ceil x=1/3
ans = 120 61 49 47 51
>> ceil x=0.5
ans = 120 61 48 46 53
>> ceil x=1/2
ans = 120 61 49 47 50
>> ceil x=1.65
ans = 120 61 49 46 54 53
>> ceil x=-1.34
ans = 120 61 45 49 46 51 52

For Floor :-
>> floor x=0.3
ans = 120 61 48 46 51
>> floor x=1/3
ans = 120 61 49 47 51
>> floor x=0.5
ans = 120 61 48 46 53
>> floor x=1/2
ans = 120 61 49 47 50
>> floor x=1.65
ans = 120 61 49 46 54 53
>> floor x=-1.34
ans = 120 61 45 49 46 51 52

For fix:-
>> fix x=0.3
ans = 120 61 48 46 51
>> fix x=1/3
ans = 120 61 49 47 51
>> fix x=0.5
ans = 120 61 48 46 53
>> fix x=1/2
ans =120 61 49 47 50
>> fix x=1.65
ans = 120 61 49 46 54 53
>> fix x=-1.34
ans = 120 61 45 49 46 51 52

Task # 02 :- (task # 05 “according to slide”)


Compare the MATLAB functions rem(x,y) and mod(x,y) for a variety
of values of x and y (try x = 3, 4, 5 and y = 3, 4,−4, 6). (Details of the
commands can be found using the help feature).
Matlab Solution :-
For rem(x,y):-
>> format rat

>> rem(3,3)
ans = 0

>> rem(3,4)
ans = 3

>> rem(4,-4)
ans = 0

>> rem(5,6)
ans = 5

For mod(x,y):-
>> format rat
>> mod (3,3)
ans = 0

>> mod(3,4)
ans = 3

>> mod(4,-4)
ans = 0

>> mod(4,6)
ans = 4

>> mod(5,6)
ans = 5

Task # 03 :- (task # 06 “according to slide”)


Evaluate the functions
1. y = x^3 + 3x^2 + 1
2. y = sinx^2
3. y = (sinx)^2
4. y = sin2x + x cos 4x
5. y = x/(x^2 + 1)
6. y = cos x/1+sin x
7. y = 1/x + x^3/(x^4 + 5x sin x)
for x from 1 to 2 in steps of 0.1

Matlab Solution :-
Values Of x:-
>> x = 1:0.1:2

x = Columns 1 through 11

1.0000 1.1000 1.2000 1.3000 1.4000 1.5000 1.6000 1.7000 1.8000


1.9000 2.0000

1. y = x^3 + 3x^2 + 1
>> y = x.^3 + 3*x.^2 + 1

y = Columns 1 through 11
5.0000 5.9610 7.0480 8.2670 9.6240 11.1250 12.7760 14.5830 16.5520
18.6890 21.0000
2. a = sinx^2
>> a = sin(x).^2

a = Columns 1 through 11

0.7081 0.7943 0.8687 0.9284 0.9711 0.9950 0.9991 0.9834 0.9484


0.8955 0.8268

3. b = (sinx)^2
>> b = (sin(x)).^2

b = Columns 1 through 11

0.7081 0.7943 0.8687 0.9284 0.9711 0.9950 0.9991 0.9834 0.9484


0.8955 0.8268
4. c = sin2x + x cos 4x
>> c = sin(2*x) + x*cos(4*x)
c = Columns 1 through 11

0.7081 0.7943 0.8687 0.9284 0.9711 0.9950 0.9991 0.9834 0.9484


0.8955 0.8268
5. d = x/(x^2 + 1)
>> d = x/(x.^2+1)

d = 0.4390
6. e = cos x/1+sin x
>> e = cos(x)/(1 + sin(x))
e = 0.0328
7. f = 1/x + x^3/(x^4 + 5x sin x)
>> f = 1/x + x^3/(x^4 + 5x sin x)

f = Columns 1 through 11

0.7081 0.7943 0.8687 0.9284 0.9711 0.9950 0.9991 0.9834 0.9484


0.8955 0.8268

Task # 04 :- (task # 07 “according to slide”)


Evaluate the function
y = x/(x +( 1/x^2))

for x = 3 to x = 5 in steps of 0.01.

Matlab Solution :-
>> x=3:0.01:5;
>> y = ((x)./(x)+(1)./(x.^2))
y = Columns 1 through 201

1.1111 1.1104 1.1096 1.1089 1.1082 1.1075 1.1068 1.1061 1.1054


1.1047 1.1041 1.1034 1.1027 1.1021 1.1014 1.1008 1.1001 1.0995
1.0989 1.0983 1.0977 1.0970 1.0964 1.0959 1.0953 1.0947 1.0941
1.0935 1.0930 1.0924 1.0918 1.0913 1.0907 1.0902 1.0896 1.0891
1.0886 1.0881 1.0875 1.0870 1.0865 1.0860 1.0855 1.0850 1.0845
1.0840 1.0835 1.0831 1.0826 1.0821 1.0816 1.0812 1.0807 1.0803
1.0798 1.0793 1.0789 1.0785 1.0780 1.0776 1.0772 1.0767 1.0763 1.0759
1.0755 1.0751 1.0747 1.0742 1.0738 1.0734 1.0730 1.0727 1.0723
1.0719 1.0715 1.0711 1.0707 1.0704 1.0700 1.0696 1.0693 1.0689
1.0685 1.0682 1.0678 1.0675 1.0671 1.0668 1.0664 1.0661 1.0657
1.0654 1.0651 1.0647 1.0644 1.0641 1.0638 1.0634 1.0631 1.0628
1.0625 1.0622 1.0619 1.0616 1.0613 1.0610 1.0607 1.0604 1.0601
1.0598 1.0595 1.0592 1.0589 1.0586 1.0583 1.0581 1.0578 1.0575
1.0572 1.0570 1.0567 1.0564 1.0562 1.0559 1.0556 1.0554 1.0551
1.0548 1.0546 1.0543 1.0541 1.0538 1.0536 1.0533 1.0531 1.0528
1.0526 1.0524 1.0521 1.0519 1.0517 1.0514 1.0512 1.0510 1.0507
1.0505 1.0503 1.0500 1.0498 1.0496 1.0494 1.0492 1.0489 1.0487
1.0485 1.0483 1.0481 1.0479 1.0477 1.0475 1.0473 1.0471 1.0469
1.0466 1.0464 1.0462 1.0460 1.0459 1.0457 1.0455 1.0453 1.0451
1.0449 1.0447 1.0445 1.0443 1.0441 1.0440 1.0438 1.0436 1.0434
1.0432 1.0430 1.0429 1.0427 1.0425 1.0423 1.0422 1.0420 1.0418
1.0416 1.0415 1.0413 1.0411 1.0410 1.0408 1.0406 1.0405 1.0403
1.0402 1.0400

Task # 05 :- (task # 08 “according to slide”)


Evaluate the function
y = 1/x^3 +1/x^2 +3/x

for x = −2 to x = −1 in steps of 0.1.


Matlab Solution :-
>> x = -2:0.1:-1;
>> y=(1./x.^3)+(1./x.^2)+(3./x)
y = Columns 1 through 11
-1.3750 -1.4477 -1.5295 -1.6222 -1.7285 -1.8519 -1.9971 -2.1711 -2.3843
-2.6521 -3.0000

Task # 06 :- (task # 09 “according to slide”)


The following code is supposed to evaluate the function
f(x) = x^2 cos πx/(x^3 + 1)(x + 2)
for x ∈ [0, 1] (using 200 steps). Correct the code and check this by evaluating
the function at x = 1 using f(200) which should be −1/6.

Matlab Solution :-
>> x = linspace(0,1,200);
>> g = x.^3+1;
>> h = x+2;
>> z = x.^2;
>> y = cos (x.*pi);
>> f = (y.*z)/(g.*h)

f = -0.0872

Task # 07 :- (task # 10 “according to slide”)


Debug the code which is supposed to plot the polynomial x4−1
between x = −2 and x = 2 using 20 points.

Matlab Solution :-
First Code:
>> x=-2:20:2;
>> c=[1 0 0 -1];
>> y=polyval(c,x);
>> plot(y,x)

Figure:
Second Code
>> x=-2:0.1:2;
>> c=[1 0 0 -1];
>> y=polyval(c,x);
>> plot(y,x)

Figure:

You might also like