Ask Your Question

Fjolfrin's profile - activity

2020-05-01 21:34:23 +0100 received badge  Popular Question (source)
2019-01-10 10:03:49 +0100 asked a question Number of elements in vector

I want to implement the Gauss-Seidel method for solving linear systems for a university project, and I'm stuck on how to get the number of elements in the product vectorb = A * x. Here's my code:

def Gauss_Seidel(m, v, initial):

n = v.size() <---------
prev = vector(RR, n)
nex = vector(RR, n)

prev = initial

while abs(inf_vector_norm(nex) - inf_vector_norm(nex)) > 0.1:
    for i in xrange(n):
            s1 = sum(m[i,j] * nex[j] for j in xrange(i))
            s2 = sum(m[i,j] * prev[j] for j in xrange(j+1, n+1))
            nex[i] = (v[i] - s1 - s2) / m[i,i]
return nex

In the code, 'm' is the input matrix, 'v' is the product of the matrix and our variable vector, and 'initial' is the starting guess for Gauss-Seidel's Method. The line pointed by that arrow is the one I'm interested in, and it's showing what I want to accomplish. I searched for it on Sage textbooks, but they are oddly silent on the matter of vectors. Any ideas? Also any other recommendations and advice on my code is more than welcome.

2019-01-08 11:42:16 +0100 commented answer Generic Symbolic function as input in actual funciton

Ok got it. Again many thanks!

2019-01-08 11:06:01 +0100 commented answer Generic Symbolic function as input in actual funciton

Thanks a lot! I have the exact same results. After I fixed the error thing I also get a logical result on this scope too:

0

(0.8571428346650166, 7, 9.64919707358014e-05)

Just to clarify, because I can't get the "%time" function you mentioned, what is the syntax of it? I tried to even copy an example from Sage's documentation, however I can't seem to get it to work. I have the "time" library imported. Meaning what I type exactly to get it to work? This is what I get for typing this: %time Newton_Raphson(f, 0)

Error in lines 48-48
Traceback (most recent call last):
File "/cocalc/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 1188, in execute flags=compile_flags) in namespace, locals
File "", line 1, in <module
File "/cocalc/lib/py
2019-01-08 10:53:57 +0100 received badge  Scholar (source)
2019-01-08 10:53:56 +0100 received badge  Supporter (source)
2019-01-07 19:32:38 +0100 received badge  Student (source)
2019-01-07 15:10:07 +0100 commented question Generic Symbolic function as input in actual funciton

Could you explain more about how I can use this Arb on my case? If it's not a problem, unless you want to suggest anything else not relevant to the main question, continue on the answer section, so others can find this thread more easily. I'm a newbie on sagemath so, show mercy on the poor! 😛😛

2019-01-07 15:03:49 +0100 received badge  Editor (source)
2019-01-07 15:03:05 +0100 commented question Generic Symbolic function as input in actual funciton

Yes time is imported and everything.

2019-01-07 15:01:46 +0100 commented question Generic Symbolic function as input in actual funciton

It's indented, for some reason the indent disappeared while copying the code.

2019-01-07 14:49:54 +0100 commented question Generic Symbolic function as input in actual funciton

Old habits don't change 😛😛 Yes I will remove them.

2019-01-07 14:41:33 +0100 asked a question Generic Symbolic function as input in actual funciton

So I have this assignment for university, and I'm trying to write a generic Newton's method which will take any symbolic function as input. I just don't know how to declare this on the function's inputs. Here's the function I want as input:

f(x) = 14 * x * exp(x-2) - 12 * exp(x-2) - 7 * x^3 + 20 * x^2 - 26 * x + 12

and here's the function I want to create:

def Newton_Raphson(foo, start):
    dfoo = diff(foo)
    ddfoo = diff(dfoo)
    t0 = time.time()

    while foo(start) * ddfoo(start) <= 0:
        start += 10^-6

    NR = start - foo(start) / dfoo(start)
    noNR = 1

    while abs(foo(NR)) > 10^-6:
        NR = start - foo(start)/dfoo(start)
        start = NR
        error = (NR - start)
        noNR += 1

    t1 = time.time()
    tNR = t1 - t0
    return NR, noNR, error, tNR

Any ideas on this? Also do you have any other suggestions on this code?