Sunteți pe pagina 1din 4

Indian Institute of Technology Gandhinagar

Discipline of Mathematics
MA 602
Advanced Numerical Methods in Engineering

Semester-II, 2018-19 Tutorial Sheet No.2

Plotting

1. Create x as a vector of linearly spaced values between 0 and 2π. Use an increment of π/10
between the values. Create y as sine values of x. Create a line plot of the data. Create z as cosine
values of x and plot it on the same figure
x = 0:pi/10:2*pi;
y = sin(x);
figure
plot(x,y)
z = cos(x);
hold on
plot(x,z)

figure
plot(x,y,'--
o','Color',[0,0.7,0.9],'LineWidth',2,'MarkerSize',10,'MarkerEdgeColor','r')
hold on
plot(x,z,'-k*','LineWidth',2,'MarkerSize',10,'MarkerEdgeColor','b')
title('2-D Line Plot')
xlabel('x')
ylabel('cos(x) and sin(x)')

2. Create a figure with two subplots and return the Axes objects as ax1 and ax2. Create x as a
vector of linearly spaced values between 0 and 2π. Use an increment of π/10 between the
values. Create y as sine values of 5*x. Create a line plot of the data in the top portion of the
figure. Create z as sine values of 15*x and plot it in the bottom portion of the figure.
ax1 = subplot(2,1,1); % top subplot
x = 0:pi/10:2*pi;
y = sin(5*x);
plot(ax1,x,y)
title(ax1,'Top Subplot')
ylabel(ax1,'sin(5x)')

ax2 = subplot(2,1,2); % bottom subplot


z = sin(15*x);
plot(ax2,x,z)
title(ax2,'Bottom Subplot')
ylabel(ax2,'sin(15x)')
Use the following link to learn additional features

https://in.mathworks.com/help/matlab/ref/plot.html
3. Use the meshgrid function to generate matrices X and Y. Create a third matrix, Z, and plot its
contours.
x = linspace(-2*pi,2*pi);
y = linspace(0,4*pi);
[X,Y] = meshgrid(x,y);
Z = sin(X)+cos(Y);

figure
contour(X,Y,Z)
figure
surf(X,Y,Z)

To learn more about different types of matlab plots use following link
https://in.mathworks.com/help/matlab/2-and-3d-plots.html

Conditional Statements

4. Write a function to generate a random number in range of [1,100], if it is even, divide by 2 and
display the quotient in a statement saying “Half of __ is __”.
a = randi(100, 1);
if rem(a, 2) == 0
b = a/2;
sprintf ('Half of %d is %d', a, b)
end

5. Write a function to input a number using keyboard. Use if-elseif-else condition to print whether
the number is positive, negative or zero.
yourNumber = input('Enter a number: ');

if yourNumber < 0
disp('Negative')
elseif yourNumber > 0
disp('Positive')
else
disp('Zero')
end

Alternatively, when you want to test for equality against a set of known values, use a “switch
statement”. Refer following link for switch statement

http://in.mathworks.com/help/matlab/matlab_prog/conditional-statements.html

Function
Syntax: function [y1,...,yN] = myfun(x1,...,xM)

6. Define a function in a file named stat.m that returns the mean and standard deviation of an
input vector.
function [m,s] = stat(x)
if ~isvector(x)
error('Input must be a vector')
end
n = length(x);
m = sum(x)/n;
s = sqrt(sum((x-m).^2/n));
end

Call the function from the command line.


values = [12.7, 45.4, 98.9, 26.6, 53.1];
[ave,stdev] = stat(values)

7. Define a script in a file named integrationScript.m that computes the area under the curve from
0 to π. Include a local function that defines the integrand, y = sin(x)3.
% Compute the area under the curve from 0 to pi.
xmin = 0;
xmax = pi;
f = @myIntegrand;
a = integral(f,xmin,xmax)

function y = myIntegrand(x)
y = sin(x).^3;
end

Loop Control Statements


for statements loop a specific number of times, and keep track of each iteration with an
incrementing index variable.

while statements loop as long as a condition remains true.

break, terminates execution of for or while loop.

return, returns control to invoking function.

continue, passes control to next iteration of for or while loop.

8. Display the multiples of 7 from 1 through 50. If a number is not divisible by 7, use continue to
skip the sprintf statement and pass control to the next iteration of the for loop.
for n = 1:50
if mod(n,7)
continue
end
disp(['Divisible by 7: ' num2str(n)])
end

9. Sum a sequence of random numbers until the next random number is greater than an upper
limit. Then, exit the loop using a break statement.
limit = 0.8;
s = 0;

while 1
tmp = rand;
if tmp > limit
break
end
s = s + tmp;
end
10. In your current working folder, create a function, findSqrRootIndex, to find the index of the
first occurrence of the square root of a value within an array. If the square root isn't found, the
function returns NaN. Use return command to implement this.
function idx = findSqrRootIndex(target,arrayToSearch)

idx = NaN;
if target < 0
return
end

for idx = 1:length(arrayToSearch)


if arrayToSearch(idx) == sqrt(target)
return
elseif idx == length(arrayToSearch)
idx = NaN;
end
end

At the command prompt, call the function.


A = [3 7 28 14 42 9 0];
b = 81;
findSqrRootIndex(b,A)

S-ar putea să vă placă și