23MAT107 Calculus Lab2

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

Amrita School of Engineering, Bengluru-35

23MAT107
Calculus
Lab Practice Sheet-2

Plotting of single variable functions

• x=-10:0.01:10; y=x.^2; plot(x, y) Plots the function f(x)=x2 in [-10,10] by creating


vectors x and y
• x=linspace(-10,10,2000); y=x.^2; plot(x, y) linspace(a,b,c) creates a vector with ‘c’ equally
distributed points in (a,b).

➢ t=-10:0.01:10; plot(t, t.^2) Plots the function f(x)=x2 in [-10,10] using the
parametric form of the function

➢ t=0:0.0001:2*pi; plot(cos(t), sin(t)) Plots the unit circle with centre at origin using
parametric form

➢ x=-10:1:10; y=x.^2; stem(x, y) Gives the stem plot of the function f(x)=x2 in [-10,10]

➢ ezplot('x^2', [-10, 10]) Plots the function f(x)=x2 in [-10,10]


But this command is not recommended much to be used

To Plot a line segment from a point A(a1, a2) to a point B(b1, b2)

➢ plot([a1, b1],[a2, b2])


➢ plot([1, 3], [2, 4]) will plot a line from (1, 2) to (3, 4)
➢ plot([0,10], [0, 0]) will plot 𝑋 axis from (0, 0) to (10, 0)
➢ plot([0, 0], [-5, 5]) will plot 𝑌 axis from (0, -5) to (0, 5)
Creation of a script file(M-file) for plotting

➢ Click on on top left of the MATLAB window, to create a new script file(m-file) and type
the following in it. After typing save the file as circle and run it. Look out for the figure which
appears in the figure window.
plots a circle with unit radius
theta = linspace(0,2*pi,100);
x=cos(theta); centred at the origin with
y=sin(theta); title and labels for axes.
plot(x,y)
axis('equal')
xlabel('x')
ylabel('y')
title('Circle of unit radius centred at the origin')
➢ Create a script file(M-file), type the following in it, save the file as multiple plot and execute it. Look
out for the figure which appears in the figure window

x=-4:0.001:4; plots multiple curves in the


y=x; same figure with different
plot(x,y,'Color','red') colours and linestyles also
hold on
y=x.^2; plot(x, y,'linestyle','--') giving the length of the axes.
hold on
plot(x, sin(x), 'Color','green', 'linestyle', '-.') Command ‘hold on’ is used
axis([-4 4 -4 4]) when multiple curves need to
be plotted in the same figure
➢ Create a script file(M-file), type the following in it, save the file as helix and execute it. Look out for
the figure which appears in the figure window

t = -4*pi:pi/50:4*pi;
plot3(sin(t), cos(t), t) plots a three-dimensional curve
grid on
- circular helix using parametric
axis square
xlabel('x') representation with labelling of
ylabel('y') axis and title for the figure
zlabel('z')
title( ‘Circular helix’)

Plotting of segmented functions:


2 − 𝑥, 0 ≤ 𝑥 ≤ 2
% Code to plot the function: 𝑓(𝑥) = { }
𝑥 − 2, 2 < 𝑥 ≤ 4
x=linspace(0,4,500);
y1=(x<=2) .* (2-x);
y2=(x>2) .* (-2+x);
y=y1+y2;
plot(x,y)

Creation of live script file


Live script file will be more convenient to do the practise questions and assignments as you can save the
questions, the MATLAB code for that question and the output together in the live script.

Click on top left of the MATLAB window, to create a new live script file. You can see the videos
given in the link below, to learn more about how to easily work with live scripts.
https://www.youtube.com/watch?v=tNcSpyCa6bc (this video also starts with basic MATLAB)
https://in.mathworks.com/videos/using-the-live-editor-117940.html
Practise Questions
1. Plot 𝑦 = 𝑠𝑖𝑛𝑥, 𝑥 = −2𝜋 𝑡𝑜 2𝜋.
2. Plot 𝑦 = 𝑡𝑎𝑛𝑥, 𝑥 = −2 𝑡𝑜 2.
3. Write a programme to plot a circle with radius 5 and centre at (1,1).
4. Write a programme to plot the ellipse (𝑥 − 3)2 + 9(𝑦 − 5)2 = 9.
(Use parametric representation).
𝑥2 𝑦2
5. Write a programme to plot the ellipse 25 + = 1 in the second quadrant.
9

6. Plot 𝑦 = 𝑠𝑖𝑛𝑥 and 𝑦 = 𝑐𝑜𝑠𝑥 in the same window with different colours and different line
styles between −4𝜋 to 4𝜋. Ensure that the X and Y axis are seen in the plot.
7. Solve the given linear system geometrically using MATLAB.
𝑥 + 𝑦 = 6; 2𝑥 – 𝑦 = 9.
8. Solve the given linear system geometrically as well as by using X= A\B in MATLAB.
𝑥 + 𝑦 + 𝑧 = 6; 2𝑥 – 𝑦 + 𝑧 = 5; 3𝑥 + 2𝑦 − 5𝑧 = 8.
9. Plot the 3-dimensional curve given by 𝑦 = 𝑥 and 𝑧 = 𝑥 2 in the interval 𝑥 𝜖 (−100,100).
10. Plot the 3-dimensional curve given by 𝑦 = 𝑠𝑖𝑛𝑥 and 𝑧 = 𝑥 2 in the interval 𝑥 𝜖 (−100,100).
11. Plot the 3-dimensional elliptical helix, which has the parametric representation as
̅̅̅̅̅ = [5𝑐𝑜𝑠𝑡, 3𝑠𝑖𝑛𝑡, 𝑡 + 1], −4𝜋 < 𝑡 < 4𝜋
𝑟(𝑡)
̅̅̅̅̅ = [𝑐𝑜𝑠𝑡, 𝑠𝑖𝑛𝑡, 𝑡], −4𝜋 < 𝑡 < 4𝜋 in different colours in the
along with the circular helix 𝑟(𝑡)
same figure window.
𝑥, 0 ≤ 𝑥 ≤ 1
12. Plot the function: 𝑓(𝑥) = { 1, 1 < 𝑥 ≤ 2 }.
3 − 𝑥, 2 ≤ 𝑥 ≤ 3
1, −10 ≤ 𝑥 ≤ −1
13. Plot the function: 𝑓(𝑥) = { −𝑥, − 1 < 𝑥 ≤ 1 },
𝑥 − 2, 1 ≤ 𝑥 ≤ 10
3𝜋
sin 𝑥, − 2 ≤ 𝑥 ≤ 0
𝜋
14. Plot the function: g(𝑥) = 2𝑥, 0 < 𝑥 ≤ 2
𝜋 3𝜋
𝜋 − cos 𝑥 , 2 ≤ 𝑥 ≤
{ 2}
15. Draw a line segment from a point 𝐴(𝑥, 𝑦) to 𝐵(𝑦, 𝑧) using a single command in MATLAB,
where 𝑥 is the date, 𝑦 is the month and 𝑧 is the year (last two digits) in your date of birth. Also
have the 𝑋 axis and 𝑌 axis in the figure.

You might also like