# Problem With Matrix. (help)

4 replies to this topic

### #1 Felipe Riquelme

Felipe Riquelme

Newbie

• Members
• 11 posts
• Gender:Male
• Location:Chile

• Calculators:

Posted 24 October 2007 - 01:54 AM

Problem with matrix.
(To resolve newtom-raphson)

In LUA9E running the code in CPLUA.EXE
The results are correct but CPLua.cpa are incorrect ...

My OS is V3.02

--------------------------------------------------------

Tengo un problema con Cplua0.9E no me entrega los resultados correctos de la matriz
y el CPlua.exe la aplicacion para windows si me da los resultadfos correctos

The code

require("cas")
F=input("ingrese la funcion")
f1=cas(F)
a=input("x0=")
ite=input("numero de Iteraciones")
d=cas.diff(F,"x")
f2=cas(d)
MZ={}
for n=2,ite do
MZ[n]={}
MZ=a
MZ[n]=MZ[n-1]-f1(MZ[n-1])/f2(MZ[n-1])
print(MZ[n-1])
end

http://www.udec.cl/~friquelme/save.mcs

### #2 Felipe Riquelme

Felipe Riquelme

Newbie

• Members
• 11 posts
• Gender:Male
• Location:Chile

• Calculators:

Posted 24 October 2007 - 10:35 PM

for example ingrese la funcion: x^2-2
X0=1
numero de iteraciones=10

1
1.5
1.4166666666667
1.4142156859408
1.4142135623155
1.4142135626886
1.4142135620617
1.4142135624348
1.4142135628079
Done.

Press [<-] or EXE' />...

In CPlua.cpa shows this error

1
1.5
Rumtime error:
main:12: attempt to
perform arithmetic on a nil
value
Press [<-] or EXE' />...

I do not understand to be someone can help me
____________________________________________
no entiendo a que se debe alguien me puede ayudar

### #3 Felipe Riquelme

Felipe Riquelme

Newbie

• Members
• 11 posts
• Gender:Male
• Location:Chile

• Calculators:

Posted 30 October 2007 - 06:48 PM

That is not happening but nobody responds? ? ?
Apparently those who know more lua not help others o quizas sea por mi mal ingles

### #4 Stiftblei

Stiftblei

Newbie

• Members
• 3 posts

• Calculators:

Posted 30 October 2007 - 08:29 PM

That error (in your example) occurs because of f1(MZ[n-1]) gives nil when n=4 (and therefore MZ[n-1]=1.41666...7), which is weird and seems like a bug:

require("cas")
f=cas("x^2-2")
print(f(a)) --replace a with a number

It gives nil for 1.41666 but not for 1.42666 nor 2.4166. Nil for 1.4144 but nor for 1.41999... If you use x^2+2, 1.41666 won't give nil... That is, on OS 3.02 (w/ CPLua .9E)
One way to resolve the problem would be:

require("cas")
F=input("ingrese la funcion")
a=input("x0=")
ite=input("numero de Iteraciones")
d=cas.diff(F,"x")
MZ={}
MZ=a --MZ won't change in the loop, so no need to put it into it
for n=2,ite do
--MZ[n]={} I can't see what this would be good for as MZ[n] is going to be overwritten in the very next line anyway...
MZ[n]=MZ[n-1]-cas(F .. "|x=" .. tostring(MZ[n-1]))/cas(d .. "|x=" .. tostring(MZ[n-1]))
print(MZ[n-1])
end

By the way, I got a mem error while running the second code mixed with the first one multiple times with different values for d.

### #5 Felipe Riquelme

Felipe Riquelme

Newbie

• Members
• 11 posts
• Gender:Male
• Location:Chile

• Calculators:

Posted 31 October 2007 - 04:43 AM

Thank you for the correction.
Up here all goes well

Muchas Gracias

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users