Semilog & Linear

Semilog & Linear

  1. Plot the functions x, x3, ex and ex^2 together over the interval 0 < x < 4 …
    on rectangular (linear) coordinates
    b. on semilog scale (logarithm on the y-axis)
    c. on log-log scale
    Be sure to use an appropriate mesh of x values to get a smooth setof curves.
  1. Make a good plot (i.e., a non-choppy plot) of the function
    f(x) = sin(1/x)
    for 0.01 < x < 0.1.
    Write programs that implement the following two functions:
  1. h(T) = T – 10 when 0 < T < 100
    = 0.45 T + 900 when T > 100
    Test cases: a. T = 5, h = -5
    T = 110, h = 949.5
  1. f(x) = -1 if x < 0
    = 0 if x = 0
    = 1 if x > 0
    Compare your results to the MATLAB function sign.
  1. Create an M-by-N array of random numbers (use rand). Move through thearray, element by element, and set any value that is less than 0.2 to 0 and anyvalue that is greater than (or equal to) 0.2 to 1.
  1. Given x = [4 1 6] and y = [6 2 7], compute the following arrays
    aij = xiyj
    b. bij = xi/yj
    c. ci = xiyi, then add up the elements of c.
    d. dij = xi/(2 + xi + yj)
    e. eij = reciprocal of the lesser of xi and yj
  1. Write a script that will use the random-number generator rand to determine the
    following:
    The number of random numbers it takes to add up to 20 (or more).
    b. The number of random numbers it takes before a number between 0.8 and
    0.85 occurs.
    c. The number of random numbers it takes before the mean of those numbers
    is within 0.01 of 0.5 (the mean of this random-number generator).
    It will be worthwhile to run your script several times because you are dealing
    with random numbers.
  1. The Fibonacci numbers are computed according to the following relation:
    F
    n = Fn-1 + Fn-2
    with F0 = F1 = 1.
    Compute the first 10 Fibonacci numbers.
    b. For the first 50 Fibonacci numbers, compute the ratio
    F
    n / Fn-1
    It is claimed that this ratio approaches the value of the golden mean
    ( (1 + sqrt(5))/2 ). What do your results show?
    In the following exercises use the dot operator where appropriate:
  1. Given the array A = [2 7 9 7 ; 3 1 5 6 ; 8 1 2 5], provide the command that will
    assign the even-numbered columns of A to an array called B
    b. assign the odd-numbered rows to an array called C
    c. convert A into a 4-by-3 array (using reshape)
    d. compute the reciprocal of each element of A
    e. compute the square-root of each element of A
  1. Create a vector x with the elements …
    2, 4, 6, 8, …
    b. 10, 8, 6, 4, 2, 0, -2, -4
    c. 1, 1/2, 1/3, 1/4, 1/5, …
    d. 0, 1/2, 2/3, 3/4, 4/5, …
  1. Create a vector x with the elements,
    x
    n = (-1)n+1/(2n-1)
    Add up the elements of the version of this vector that has 100 elements.

Solution 

f.m 

function y=f(x)

if x<0

y=-1;

end

if x==0

y=0;

end

if x>0

y=1;

end

end 

h.m 

function y = h(T);

if T>0 && T<100

y=T-10;

end

if T>100

y=0.45*T+900;

end

if T<0

fprintf(‘not correct input’);

end

end 

q1.m 

x=linspace(0,4,1000);

y1=x;

y2=x.^3;

y3=exp(x);

y4=exp(x.^2);

plot(x,y1);

hold on;

plot(x,y2);

plot(x,y3);

plot(x,y4);

legend(‘x’, ‘x^3’, ‘exp(x)’, ‘exp(x^2)’);

title(‘rectangular coordinates’);

grid on;

figure();

semilogx(x,y1);

hold on;

semilogx(x,y2);

semilogx(x,y3);

semilogx(x,y4);

legend(‘x’, ‘x^3’, ‘exp(x)’, ‘exp(x^2)’);

title(‘semi-log scale’);

grid on;

figure();

loglog(x,y1);

hold on;

loglog(x,y2);

loglog(x,y3);

loglog(x,y4);

legend(‘x’, ‘x^3’, ‘exp(x)’, ‘exp(x^2)’);

title(‘log-log scale’);

grid on; 

q2.m 

x=linspace(0.01,0.1,1000);

plot(x,sin(1./x)); 

q3.m 

h(5)

h(110) 

q4.m 

if f(6)==sign(6)  %u can check for other positive values

fprintf(‘correct\n’);

end

if f(-10)==sign(-10)  %u can check for other negative values

fprintf(‘correct\n’);

end

if f(0)==sign(0)

fprintf(‘correct\n’);

end 

q5.m 

m=10; %u can set ur own m and n

n=10;

x=rand(m,n);

for i=1:1:m

for j=1:1:n

if x(i,j)<0.2

x(i,j)=0;

else x(i,j)=1;

end

end

end 

q6.m 

x=[4 1 6];

y=[6 2 7];

for i=1:1:3

for j=1:1:3

a(i,j)=x(i)*y(j);

end

end

for i=1:1:3

for j=1:1:3

b(i,j)=x(i)/y(j);

end

end

for i=1:1:3

c(i)=x(i)*y(i);

end

sum(c)

for i=1:1:3

for j=1:1:3

d(i,j)=x(i)/(2+x(i)+y(j));

end

end

for i=1:1:3

for j=1:1:3

e(i,j)=1/min(x,y);

end

end 

q7.m 

%part a

sum=0;

count = 0;

while sum<=20

sum=sum+rand(1,1);

count=count+1;

end

sum  %prints value of sum

count    %prints no. of random nos.

%part b

count1=0;

while(1)

if rand(1,1)>0.8 && rand(1,1)<0.85

break;

end

count1=count1+1;

end

count1   %prints no. of random nos.

%part c

sum=0;

count2=0;

mean=0;

while(1)

sum=sum+rand(1,1);

count=count+1;

mean=sum/count; %calculates mean;

if mean>0.01 && mean<0.5

break;

end

end

count2

mean 

q8.m 

clear

%part a

f=zeros(10,1);

f(1)=1;  %index starts from 1 here

f(2)=1;

for i=3:10

f(i)=f(i-1)+f(i-2);

end

%part b

f1=zeros(50,1);

f1(1)=1;  %index starts from 1 here

f1(2)=1;

for i=3:50

f(i)=f(i-1)+f(i-2);

end

%50 fibonacci nos. generated

for i=2:50

x(i-1)=f(i)/f(i-1);

end

plot(x); % from the plot it is clear that x approaches 1+sqrt(5)/2, u can check that 

q9.m

 clear

A=[2 7 9 7; 3 1 5 6; 8 1 2 5];

%part a

i=1;

for j=1:1:4

if mod(j,2)==0

B(:,i)=A(:,j);

i=i+1;

end

end

%part b

j=1;

for i=1:1:3

if mod(i,2)~=0

C(j,:)=A(i,:);

j=j+1;

end

end

%part c

A1=transpose(A);

% part d

A2=1./A;

A2  %displays the reciprocal values

%part e

A3=sqrt(A)    %displays square root values 

q10.m 

clear;

%part a

a=2:2:1000; %u can decide the final value

%part b

b=10:-2:-4

%part c

c1=linspace(1,100,100)

c=1/c1;

%part d

n=100; %u can change this

for i=1:1:n

d(i)=(i-1)/i;

end 

q11.m 

clear

for i=1:1:100

x(i)=((-1)^(i+1))/(2*i-1);

end

sum(x)