Noc
Noc

Reputation: 519

MATLAB Functions using Vectors

I'm attempting to evaluates the function x -> (e^x - 1) / x at the following seven values:

1, .5, .1, .01, .001, .00001, .0000001

My code is:

x = [1,.5, .1, .01, .001, .00001, .0000001];
y = (exp(x)-1)/x

The issue is that this yields only one value for y, namely:

y =
   1.629441654061645

I noticed that if I remove /x, it gives me seven values that correspond to each value of x (albeit the wrong ones).

Why is this the case, exactly?

Upvotes: 3

Views: 116

Answers (1)

Junuxx
Junuxx

Reputation: 14251

/ gives you a matrix division, while what you want is an elementwise division. That's done with the ./ operator:

 y = (exp(x)-1)./x

Upvotes: 6

Related Questions