cancel
Showing results forΒ 
Search instead forΒ 
Did you mean:Β 
cancel
Showing results forΒ 
Search instead forΒ 
Did you mean:Β 

Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X

Using Mathcad to solve a system of non-linear equations using Newton's method

JA_9803374
6-Contributor

Using Mathcad to solve a system of non-linear equations using Newton's method

Hello,

I am struggling in implementing a problem that  implements Newton's method to solve a system of two non-linear equations:

π‘₯^4βˆ’π‘¦π‘₯^3=1

6cos(π‘₯+1)βˆ’π‘¦=2

I am trying to implement 

π‘₯^(𝑛+1)=π‘₯^(𝑛)βˆ’π½(π‘₯(𝑛))βˆ’1βˆ™πΉ(π‘₯^(𝑛))with 𝐽(π‘₯^(𝑛))^βˆ’1is the evaluation of the inverse of the Jacobian matrix and 𝐹(π‘₯(𝑛))is the output of the equations.

I have done most of the iteration work I believe in the function but I am stuck on how to manipulate the values to come up with the intersection points of these two functions.

 

Here is my code:

ACCEPTED SOLUTION

Accepted Solutions
Werner_E
25-Diamond I
(To:JA_9803374)

If you are just looking for a solution, you sure would use Primes built-in methods ("root" function or a solve block)

Werner_E_0-1612869705660.png

 

But I guess this is an exercise in programming and implementing an algorithm in Prime.
You also want the whole series of iteration steps as the result.

 

I spotted a couple of errors in your sheet:

 

1) The guess values 0;0 result in a non-invertible Jacobi-Matrix and so your function must fail

 

2) You defined f as a function with two arguments (x and y) but in your Newton function you used the function argument f as a function with just one vector argument. You should rewrite your input function f to accept a vector as its argument and do the very same with your function J. That way you may also be able to deal with more than two variables, using the very same function "Newton". You may just redefine J(x):=J(x[1,x[2) to save typing the definition from anew.x

 

At the top you are collecting the current variable values and the number of the iteration step in a matrix "result"
3) You forgot about the second matrix index (should bi "i") in result
4) When writing x_i you used the literal index instead of the vector index (Thats what the error message is trying to tell you)
Furthermore "result" in the for-loop is accidentally labelled as "function" - it must be manually re-labelled as "Variable"

 

5) You completely missed the correct iteration step using J(x), etc. You simply calculated the new x-value being the result of f(x).

 

If you correct those errors and you provide meaningful guess values, your routine should work OK (I renamed you function f to F, so I can use the previously defined function f (and g) to check the result using Primes root-function.

Werner_E_0-1612865728855.png

 

View solution in original post

10 REPLIES 10
ttokoro
20-Turquoise
(To:JA_9803374)

image.pngimage.pngimage.png

Sorry, the root with 2 arguments is not the Newton Method

The Newton method for two equation is

Moscow Power Engineering Institute: Mathcad Calculation Server (mpei.ac.ru)

Newton-2.png

LucMeekes
23-Emerald III
(To:JA_9803374)

What is the main purpose?

1. Solve the set of equations.

2. (Learn how to) implement the method.

 

One solution of the set is easily found with Mathcad, without programming.

x is close (within a %) to 2/3, y=6*cos(1+x)-2

 

Succes!
Luc

 

 

symbolic solution:

LucMeekes_0-1612870876491.png

 

 

Here's one way of doing it (I've used M15, but you should be able to use the method in Prime)::

NonLin.jpg

 

Werner_E
25-Diamond I
(To:JA_9803374)

If you are just looking for a solution, you sure would use Primes built-in methods ("root" function or a solve block)

Werner_E_0-1612869705660.png

 

But I guess this is an exercise in programming and implementing an algorithm in Prime.
You also want the whole series of iteration steps as the result.

 

I spotted a couple of errors in your sheet:

 

1) The guess values 0;0 result in a non-invertible Jacobi-Matrix and so your function must fail

 

2) You defined f as a function with two arguments (x and y) but in your Newton function you used the function argument f as a function with just one vector argument. You should rewrite your input function f to accept a vector as its argument and do the very same with your function J. That way you may also be able to deal with more than two variables, using the very same function "Newton". You may just redefine J(x):=J(x[1,x[2) to save typing the definition from anew.x

 

At the top you are collecting the current variable values and the number of the iteration step in a matrix "result"
3) You forgot about the second matrix index (should bi "i") in result
4) When writing x_i you used the literal index instead of the vector index (Thats what the error message is trying to tell you)
Furthermore "result" in the for-loop is accidentally labelled as "function" - it must be manually re-labelled as "Variable"

 

5) You completely missed the correct iteration step using J(x), etc. You simply calculated the new x-value being the result of f(x).

 

If you correct those errors and you provide meaningful guess values, your routine should work OK (I renamed you function f to F, so I can use the previously defined function f (and g) to check the result using Primes root-function.

Werner_E_0-1612865728855.png

 

JA_9803374
6-Contributor
(To:Werner_E)

Hey, I'm just wondering if this finds the intersection points of these two functions which gives 4 different sets of values. Sorry, I'm pretty new to Mathcad so I'm still learning how to implement all these things.

Werner_E
25-Diamond I
(To:JA_9803374)


@JA_9803374 wrote:

Hey, I'm just wondering if this finds the intersection points of these two functions which gives 4 different sets of values. Sorry, I'm pretty new to Mathcad so I'm still learning how to implement all these things.


Prime is not able to find all four solutions automatically.

You have to provide either appropriate guess values or ranges, as ttokoro's picture had shown.

You can use the "root" or a solve block with "find".
In the example below I turned the solve block into a function of the guess value for x (the guess for y is constant 0).

Given the estimates you provided its possible to find all four points of intersection:

Werner_E_0-1612903079009.png

But of course you can find the same values using your own "Newton" function.
I assume you already corrected the above mentioned errors.

 

JA_9803374
6-Contributor
(To:JA_9803374)

JA_9803374_0-1612908273961.pngJA_9803374_1-1612908334818.png

 

So I am pretty sure I fixed most of the problems and I am on a better path now. However I am struggling in how to implement the iterations needed to manipulate my variables currently. I feel like I'm close to figuring it out.

Thank you so much for the help!

Werner_E
25-Diamond I
(To:JA_9803374)


However I am struggling in how to implement the iterations needed to manipulate my variables currently. I feel like I'm close to figuring it out.

 


I am not sure what you mean and what the problem is!?

Without seeing your sheet its hard to tell, but I guess that the iteration formula may be placed at the wrong position here

Werner_E_0-1612910973266.png

Furthermore it should rather be "x <-- ..." as as you show it you are overwriting what was collected in "result" so far. I guess the correct place to put this statement is where you had x <--- f(x) in your original sheet.

 

I also overlooked that your termination condition contains two errors:

1) the closing parenthesis ")" should be placed before the "<"

2) the arrow (vectorization) should contain the absolute value, too, otherwise it would have no effect.

 

Here is an example how to use your Newton-function to solve three non-linear equations in three variables. I compared it to Primes solve block. If you play around with the guess values you will notice, that "Newton" is more sensible with respect to the guess values and may come up with a different solution compared to Prime's solve block using the same guesses. I used Prime's "Jacob" function to create the Jacobi matrix.

Werner_E_1-1612911561682.png

P6 sheet attached

 

JA_9803374
6-Contributor
(To:Werner_E)

Oh ok I see now, I think i was over complicating the function more than it needed to be. Thank you so much for all the help.

Announcements

Top Tags