Wednesday, February 25, 2026

MATLAB practice problems

MATLAB Interactive Practice (White Editor Style)

Practice Problem 1

Plot the polygonal line for:

x = (2, 4, 6, ..., 20)

y = reciprocals of y1 = (-5, -4, ..., 3)

Step 1 – Create x

Since step size is fixed (2), use colon operator.

Step 2 – Create y1

Numbers increase by 1, so step is 1.

Step 3 – Compute reciprocals

Use element-wise division: ./

Complete MATLAB Code
figure
x = 2:2:20;
y1 = -5:1:3;
y = 1 ./ y1;

plot(x,y,'o-')
title('Polygonal Line')
xlabel('x')
ylabel('y')
grid on

Practice Problem 2

Plot f(x) = sin(x) + x² on [-6,6]

First rough, then dense segmentation.

Complete MATLAB Code
% Rough segmentation
x = -6:1:6;
y = sin(x) + x.^2;
plot(x,y)

% Dense segmentation
x = -6:0.01:6;
y = sin(x) + x.^2;
plot(x,y)

Practice Problem 3

Plot two functions in same figure:

f(x) = cos(x)

g(x) = x³

Complete MATLAB Code
figure
x = -4:0.01:4;
f = cos(x);
g = x.^3;

plot(x,f,'r--','LineWidth',2)
hold on
plot(x,g,'b')
legend('cos(x)','x^3')
xlabel('x')
ylabel('y')
grid on

Practice Problem 4

Create 25 equally spaced points in [0,8] and plot y = √x

Complete MATLAB Code
figure
x = linspace(0,8,25);
y = sqrt(x);

plot(x,y,'m-o')
title('Square Root Function')
xlabel('x')
ylabel('sqrt(x)')
grid on

No comments:

Post a Comment