snack365

5 Reputation

0 Badges

12 years, 132 days

MaplePrimes Activity


These are replies submitted by snack365

@Adri van der Meer

Any idea what's wrong with this code?

> f:=x->BesselJ(0,x);

f := x -> BesselJ(0, x)

> low:=-5;

low := -5

> up:=5;

up := 5

> step:=1;

step := 1

> dp:=5;

dp := 5

> #Newton Raphson on x-cos(x)=0
> newton_raphson:=proc(eqn,x,dp)
> local x_old, x_new, counter, not_converged:
> x_old:=x:
> x_new:=x_old-(eqn(x_old)/(D(eqn)(x_old))):
> counter:=0:
> not_converged:=abs(x_old-x_new)>5*10.0^(-(dp+1)):
> while not_converged do
> x_old:=x_new:
> x_new:=evalf(x_old-(eqn(x_old)/(D(eqn)(x_old)))):
> counter:=counter+1:
> not_converged:=abs(x_old-x_new)>5*10.0^(-(dp+1)):
> od:
> print(`The root lies at the point `,x_new);
> print(`It took `,counter,` iterations to reach this root.`);
> print(`The correct value according to Maple is`,evalf(solve(BesselJ(0,z),z)));
> end:
>
> for i from low to up-step by step do
>
> a:=i:
> b:=i+step:
>
> if evalf(f(a)*f(b)) < 0 then
>
> print(`root in interval`,a,b):
> newton_raphson(f,b,5);
>
> fi:
>
> od:


 

@Adri van der Meer

Any idea what's wrong with this code?

> f:=x->BesselJ(0,x);

f := x -> BesselJ(0, x)

> low:=-5;

low := -5

> up:=5;

up := 5

> step:=1;

step := 1

> dp:=5;

dp := 5

> #Newton Raphson on x-cos(x)=0
> newton_raphson:=proc(eqn,x,dp)
> local x_old, x_new, counter, not_converged:
> x_old:=x:
> x_new:=x_old-(eqn(x_old)/(D(eqn)(x_old))):
> counter:=0:
> not_converged:=abs(x_old-x_new)>5*10.0^(-(dp+1)):
> while not_converged do
> x_old:=x_new:
> x_new:=evalf(x_old-(eqn(x_old)/(D(eqn)(x_old)))):
> counter:=counter+1:
> not_converged:=abs(x_old-x_new)>5*10.0^(-(dp+1)):
> od:
> print(`The root lies at the point `,x_new);
> print(`It took `,counter,` iterations to reach this root.`);
> print(`The correct value according to Maple is`,evalf(solve(BesselJ(0,z),z)));
> end:
>
> for i from low to up-step by step do
>
> a:=i:
> b:=i+step:
>
> if evalf(f(a)*f(b)) < 0 then
>
> print(`root in interval`,a,b):
> newton_raphson(f,b,5);
>
> fi:
>
> od:


 

Page 1 of 1