help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How do I?? function with e^x


From: Doug Stewart
Subject: Re: How do I?? function with e^x
Date: Mon, 16 Jun 2008 06:31:22 -0400
User-agent: Thunderbird 2.0.0.14 (X11/20080505)

Geko wrote:
I have been trying to create and run some functions on octave, which has become very handy to me. I was trying with a function that performs the secant method for obtaining roots. But suddenly after trying it with one equation with e^x, I started having my issue. I know that the exponent of the x are assumed depending on the position of the number in the vector/matrix; but what if the x is the exponent? Is there a way to write it like that? or should I implement to my code a way to find that x?

For reference I will add the code and the function I am trying to run.
f(x) = [(x-1)*e^x ]-2
x0 = 1
x1 = 1.5
for running the function type as: secant(1,1.5,0.0001,6)
then, type f(x).

#delta is the error we are willing to accept for octave to find when looking for the root.
#max1 is the max amount of iterations we want the octave to perform.
#function is written assuming the max amount of iterations could be 6.
function [p1,y1,err,P] = secant(p0,p1,delta,max1)
f = input("type f(x): ", "s");
f = inline(f);
epsilon = 1.0842e-19;
P(1) = p0;
P(2) = p1;
P(3) = 0;
P(4) = 0;
P(5) = 0;
P(6) = 0;
y0 = feval(f,p0);
y1 = feval(f,p1);
for k=1:max1,
  df = (y1-y0)/(p1-p0);
  if df == 0,
    dp = 0;
  else
    dp = y1/df;
  end
  p2 = p1 - dp;
  y2 = feval(f,p2);
  err = abs(dp);
  relerr = err/(abs(p2)+eps);
  p0 = p1;
  y0 = y1;
  p1 = p2;
  y1 = y2;
  P = [P;p0 p2 p1 y1 err relerr];
  if (err<delta)|(relerr<delta)|(abs(y2)<epsilon), break, end
end
disp(' P0         P2       Xi     F(Xi)    ErrAbs ErrorRelativo');
disp(P);
disp('Error Absoluto:');
disp(err);
disp('Error Relativo:');
disp(relerr);
end
------------------------------------------------------------------------

_______________________________________________
Help-octave mailing list
address@hidden
https://www.cae.wisc.edu/mailman/listinfo/help-octave
did you look at exp(x)

try help exp
and help power

octave:6> *power(2.718281828459045,3)*

ans = 20.086

octave:7> *exp(3)*

ans = 20.086

octave:8>


HTH

Doug Stewart





reply via email to

[Prev in Thread] Current Thread [Next in Thread]