slipper

44 Reputation

2 Badges

14 years, 228 days

MaplePrimes Activity


These are answers submitted by slipper

sorry,

i will start from the beginning. i have this procedure (below), which emulates the secant method for finding a solution to f(x)=0, with initial estimates x0 and x1.

iter := proc(f::mathfunc,x0::complex,x1::complex,N::integer)                                       

  local a,k:                                                                       
>                 a[0] := x0:                                                                   
>                 a[1]:= x1:                                                                  
>                  for k to N do                                                              
>                        a[0] := evalf(a[0]-f(a[0])*((a[0]-a[1])/(f(a[0])-f(a[1])))):                                
>                        print(a[0]):                                                         
>                  end do:                                                                    

> end proc:                                                                                  
>
>
>
>
> f := x->x^2-2;

                                      2
                           f := x -> x  - 2

> iter(f,1.,2,10);
 

 

I am trying to modify to obtain procedure iter2(f,x0,x1,N,gs), where gs is real number. iter2 to return first iterate x[k] for which |x[k]-k[k-1]|<gs and k<=N, if  |x[k]-k[k-1]|>gs, then an error message saying covergence not achieved.

The first two posts were my attempts to include a further statement, in the above procedure to obtain this.

 

regards

 

slip

sorry the above wasn't very clear, heres a new post.

 

iter := proc(f::mathfunc,x0::complex,x1::complex,N::integer,gs::realcons)         local a,k,n:
>                a[0] := x0:
>                a[1] := x1:
>                   for k to N do
>                        a[0] := evalf(a[0]-f(a[0])*((a[0]-a[1])/(f(a[0])-f(a[1])))):                 
>                        a[t] := a[t],a[0]:
>                          print(abs(a[t+1]-a[t])):
>                           if gs>abs(a[t+1]-a[t]) and k<=N then return
>                          print(abs(a[t+1]-a[t])):
>                       end if:
>                    end do:
> end proc:

i want to evaluate a[t] at the value for a[0], then if gs>abs(a[t+1]-a[t]) and k<=N . i want to return the first iterate a[t+1] that meets this criteria and print an out put to see if it converges.

 

many thanks for any help

 

regards

 

slip
 

Doug, Thanks alot, i can see the logic of having sm=0 , and print inside the first loop now, so that each evaluation of N is output to print. Your help greatly appreciated. regards slip

Thanks for the help Doug,

Tried a nested loop, but does not seem to evaluate as i was expecting, as in does not give the same values as original equation with N =1, also the print list is quite large, hence the # do not know how to get the (N,sm(N*20) value to show.

restart:sm := 0: n := N*20:

 for N from 1 to 30 do

                 for k from 1 to n do

                  sm := sm+(1/(k^2+3)):

# print(N,evalf(sm)):

end do:

end do:

 

 

regards

Thanks for the reply Chris,

I don't think i explained mysely to well. I am aware of the sum function in maple, but what i am trying to do is make a loop to simulate this sum function, as in the first post. But how can i get this loop to evaluate all the values of N and output a list? Or can it only be done one at a time? I know its long winded but its just a practice example.

 

regards

slip

Page 1 of 1