-1

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!

zack
  • 1
  • I'd highly suggest looking up a tutorial online somewhere - just search for "Introduction to MATLAB" or something similar into your favourite search engine. – Andrew D Sep 15 '13 at 17:26

2 Answers2

2

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)
Adriano
  • 41,576
  • Thank you so much, this works perfectly and helps me understand how to use matlab so much more. Thanks! – zack Sep 15 '13 at 21:13
  • No problem! I'm just learning MATLAB myself this semester, and this question was good practice for me. =] – Adriano Sep 15 '13 at 21:17
0

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.

horchler
  • 3,203