zallke
zallke

Reputation: 49

Having trouble plotting in matlab

I'm trying to plot a function in Matlab but all I get is a straight line.

The code I'm using is:

De = 6.22238*1e-5;
alpha = 3.0662*1e5;
re = 2.666;

r = -1:1/100:5;

V = De * (1-exp(alpha*(re-r))).^2
plot(r,V);

But all i get is a straight line. the plot is supposed to look somthing like this: http://chemwiki.ucdavis.edu/@api/deki/files/55194/=mHj1k.png?revision=1

Are the numbers too small so matlab just rounds everything off?

Please help!

Upvotes: 0

Views: 42

Answers (1)

Alldb
Alldb

Reputation: 130

some of your sub functions especially exponential functions are overflowing and this overflow cause Matlab to consider this value as infinity. In your code the output value for exp(alpha*(re-r)) will result to:

  • infinity if re >> r
  • 1 if re=r
  • 0 if re << r

To prevent this problem we use a trick which is for example if we want to calculate (very big number) * exp(very small number), at first we change the formula to equal formula: exp(Log(very big number)*(very small number)) As you can see Log(very big number) will result to smaller number and also we hope that Log(very big number) * (very small number) with result to a moderate value that will not make problem encountering exponential functions.

Upvotes: 2

Related Questions