Use Matlab to print function values at 101 equally spaced points between [0.99,1.01]

1.2k Views Asked by At

Im very new at Matlab and I can't seem to figure out this seemingly simple task. I have 3 functions f(x), g(x), and h(x) which are technically the same but formatted differently. What I need to do is print out 101 equally spaced values between [0.99,1.01] for each of these functions. Then I need to plot these values on the same graph. Does anyone know how to do this? I've never used Matlab before and I can't find instructions for doing this anywhere. Thanks in advance!

2

There are 2 best solutions below

2
On BEST ANSWER

Here's a basic template you could follow:

n = 101; a = 0.99; b = 1.01;
step = (b - a)/(n - 1);
x = [a : step : b];
% NOTE: x is a vector containing the n numbers:
% x = [a, a + (b-a)/(n-1), ..., a + (n-1)(b-a)/(n-1) = b]

% Fill in your functions here.
f = sin(2*pi/0.01 * x) + 1;
g = sin(2*pi/0.01 * x) + 2;
h = sin(2*pi/0.01 * x) + 3;

plot(x,f, x,g, x,h)
0
On

Save yourself some work. Matlab has a flexible builtin function for this: linspace. This is all you need

x = linspace(0.99,1.01,101);

Also see logspace.