Ryan Sayles
Ryan Sayles

Reputation: 3431

Writing equations in matlab

I am new to matlab and I'm working on a simple equation: y = (1/1+2x) - ((1-x)/(1+x)). In matlab I wrote it like this: (1./(1+(2.*x)))-((1-x)./(1+x)); since my x's are in a range I have to use (.) operator. My equation however is not plotting the graph as I would expect it. It is a parabola with a minimum critical point at around x = 50 which doesn't make sense. This leads me to believe I have a syntax error. If someone could help it would be much appreciated!

Here is my current code:

f = @(x) (1./(1+(2.*x)))-((1-x)./(1+x));
h =1*exp(-10);
x = (-1*exp(-6)):h:(1*exp(-6));
y = f(x);
plot(y)

Upvotes: 0

Views: 195

Answers (1)

Stewie Griffin
Stewie Griffin

Reputation: 14939

You are plotting your f(x) with default x-axis, from 1:numel(y).

To get the correct result try:

plot(x,f(x))

or

plot(x,y) 

Upvotes: 3

Related Questions