0
double a = 1, Lspan = 30, Lcable = 33, fn, fdn, dfn, j;

fn = (2 * a * (Math.Sinh(Lspan / 2 * a))) - Lcable;

fdn = (2 * (Math.Sinh(Lspan / 2 * a)) - ((Lspan / 2 * a) * Math.Cosh(Lspan / 2 * a)));

dfn = -fn / fdn;

do
    j = a + dfn;
while (dfn > 0.00000000001);

So that is my code. I know it is very badly written! I am a beginner. So here is my question, from above what I am trying to do is a newton raphson method..

xn+1 = xn - f(xn)/f'(xn)

Now i know how to calculate f(xn)/f'(xn) as shown above but what I have problems with is the xn part.

For those who do not know, with newton raphson you give a guess estimate for xn, lets say 1 and you fill it in..then the new answer you get becomes xn for the equation and you fill that in an so forth until you reach a limit. Now if you could follow that, Its very badly written to be honest! Then please I need your help!

NoviceProgrammer
  • 3,347
  • 1
  • 22
  • 32
Dave
  • 1
  • 2
    Looks awfully familiar to -> http://stackoverflow.com/questions/20223022/numerically-solving-an-equation – gleng Nov 27 '13 at 14:02
  • Honestly i have no idea what are you talking about but i googled your problem, does this link help? http://www.daniweb.com/software-development/csharp/code/369815/newton-raphson-in-c – Igor Meszaros Nov 27 '13 at 14:06
  • I know exactly what the Newton Raphson method is, but I'm not sure what the question here is. You're asking how to find xn? – doctorlove Nov 27 '13 at 14:08
  • Have you cunning chosen the variable `a` to represent the root `x` you are trying to find? – doctorlove Nov 27 '13 at 14:13
  • Where have you got +1 from? Wikipedia doesn't have a +1: http://en.wikipedia.org/wiki/Newton%27s_method – doctorlove Nov 27 '13 at 14:16
  • @doctorlove: that was my mistake in the edit that i made...now corrected – NoviceProgrammer Nov 27 '13 at 14:25
  • @NoviceProgrammer Homework tag is deprecated, please don't use it anymore (http://meta.stackexchange.com/questions/147100/the-homework-tag-is-now-officially-deprecated) – Mr47 Nov 27 '13 at 15:08
  • @Mr47: oops. Thanks for the correction. took down that comment – NoviceProgrammer Nov 27 '13 at 15:11

2 Answers2

3

I believe Newton Raphson uses

xn+1 = xn - f(xn)/f'(xn)

Maybe the +1 came out wrong in the formatting (and has now been corrected).

In this case, you loop until you get close enough updating xn as you go. I believe your code is trying to use j and a for this. Let's be boring and use x instead:

double x = 1, Lspan = 30, Lcable = 33, fn, fdn, dfn;

do
{
    fn = (2 * x * (Math.Sinh(Lspan / 2 * x))) - Lcable;

    fdn = (2 * (Math.Sinh(Lspan / 2 * x)) - ((Lspan / 2 * x) * Math.Cosh(Lspan / 2 * x)));

    dfn = -fn / fdn;

    x += dfn;
}
while (dfn > 0.00000000001);

Note - this will deal with your problem of where to loop. You need to think about the while condition - you should probably check the absolute value. What happens if it's -1000 to start with?

doctorlove
  • 18,872
  • 2
  • 46
  • 62
  • Is x being updated every time yes? I tried it but my output is 1.113 and on my calculator it shows 19.648 which I think is correct, im looking at the equations now to see if there are entered right! Thank you for the reply – Dave Nov 27 '13 at 17:35
  • @Dave have you changed the code to use abs: http://msdn.microsoft.com/en-us/library/a4ke8e73%28v=vs.110%29.aspx – doctorlove Nov 27 '13 at 18:08
  • no I didn't. Im new to all this and have not heard of that before. – Dave Nov 27 '13 at 20:34
0

Apart from everything else already told, like updating the function value and derivative value inside the loop and removing the sign from dx before comparing it to the error boundary, there is another basic problem in the code.

As written,

Lspan / 2 * a

is the same as

Lspan * 0.5 * a

Assuming that the derivative was once correct, it can be inferred that

Lspan / (2 * a)

was intended, which could also be written as

0.5 * Lspan / a
Lutz Lehmann
  • 25,219
  • 2
  • 22
  • 51