Welcome Guest! To enable all features please Login. New Registrations are disabled.

Notification

Icon
Error

Login


Options
Go to last post Go to first unread
Offline gth802s  
#1 Posted : 20 December 2023 15:40:40(UTC)
gth802s


Rank: Member

Groups: Registered
Joined: 21/11/2023(UTC)
Posts: 24
United States

Was thanked: 2 time(s) in 2 post(s)
Hi all, back with another question :-)

I am trying to write a while loop that will iterate for a variable until a certain user-defined error tolerance is reached. I started by writing the loop for a single element, and got it working fine. When I then tried to make it work using vectors, it goes through one iteration then stops. I have attached my loop as it stands; for example, the first value of m after iteration should be 0.243, not 0.183.

I am starting to think I need a nested for loop, but to be honest, I am a bit behind times when it comes to programming...

Any help is appreciated, and thanks in advance!

While Loop Not Working.sm (15kb) downloaded 4 time(s).

Edited by user 21 December 2023 16:25:12(UTC)  | Reason: Clarity

Wanna join the discussion?! Login to your SMath Studio Forum forum account. New Registrations are disabled.

Offline Razonar  
#2 Posted : 20 December 2023 18:18:08(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 1,356
Uruguay

Was thanked: 815 time(s) in 516 post(s)
Hi. Some few notes about what I think is happening. The break inside the for loop stops the for loop, not the while one. So, you must put it there. Thus, the tolerance checking must be in the while loop too, and to do that you have the vectorial function normi(x) or max(vectorize(abs(x))) for comparing with only one TOL. Finally, you need to increase the counter i outside the for loop.

BTW, looking at the Error array, it looks like there is an issue at row 2.

While Loop Not Working.sm (16kb) downloaded 9 time(s).

Best regards.
Alvaro.

Edited by user 20 December 2023 18:25:33(UTC)  | Reason: Not specified

Offline gth802s  
#3 Posted : 21 December 2023 03:04:57(UTC)
gth802s


Rank: Member

Groups: Registered
Joined: 21/11/2023(UTC)
Posts: 24
United States

Was thanked: 2 time(s) in 2 post(s)
Thanks, Alvaro. I should have caught that break being inside the for loop instead of the while one.

While playing with my simple while loop, I figured out that assigning m the value from the previous iteration is not the way to go. What works better is to start with m=0 and add a small increment (say, 0.001) to it with each iteration (so the new m=m+0.001). Theoretically, m needs to be between 0 and 1 so this is fine.

That said, when I try to implement that approach into the loop with the vectors, it just blows through all the iterations and does not stop at the specified Error threshold... I need to spend some more time and figure out what is going on.
Offline Razonar  
#4 Posted : 21 December 2023 07:03:41(UTC)
Razonar


Rank: Advanced Member

Groups: Registered
Joined: 28/08/2014(UTC)
Posts: 1,356
Uruguay

Was thanked: 815 time(s) in 516 post(s)
Originally Posted by: gth802s Go to Quoted Post
While playing with my simple while loop, I figured out that assigning m the value from the previous iteration is not the way to go. What works better is to start with m=0 and add a small increment (say, 0.001) to it with each iteration (so the new m=m+0.001). Theoretically, m needs to be between 0 and 1 so this is fine.


For a general numerical procedure to minimize or find roots the guess initial value is part of the problem start data, not a result of some reasoning, except that you study the behavior of some particular function.

For example for solving f(x) = 0, f(x) = 1 - x^2 starting with x = 0.5 you usually get x = 1, and with x = -0.5 results x = -1. That means that you must know something about the solution before starting a numerical procedure.

In your case your m independent variable is not scalar, but vectorial. You can't add m+0.01 ... you must add 0.01 for each component looking to all combinations, which actually could be done, but is a kind of brute force method. Usually you know something about the desired result and use that to select "good" guess values for each component.

Originally Posted by: gth802s Go to Quoted Post
That said, when I try to implement that approach into the loop with the vectors, it just blows through all the iterations and does not stop at the specified Error threshold... I need to spend some more time and figure out what is going on.


Not stopping just because of an issue with m[2]. The Error for other components decreases very quickly. Just check if it needs a different guess value or why with the values of the others variables it not converges to a solution.

Best regards.
Alvaro.
Offline gth802s  
#5 Posted : 21 December 2023 10:28:16(UTC)
gth802s


Rank: Member

Groups: Registered
Joined: 21/11/2023(UTC)
Posts: 24
United States

Was thanked: 2 time(s) in 2 post(s)
It seems it is not just the issue with m[2]. The values calculated by that loop are not the correct ones; the correct values should be:

0.234,0.522,0.564,0.524,0.528,0.527,0.535,0.564,0.614,0.621

Originally Posted by: Razonar Go to Quoted Post


Not stopping just because of an issue with m[2]. The Error for other components decreases very quickly. Just check if it needs a different guess value or why with the values of the others variables it not converges to a solution.

Best regards.
Alvaro.
Offline Jean Giraud  
#6 Posted : 22 December 2023 01:05:35(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,866
Canada

Was thanked: 981 time(s) in 809 post(s)
Originally Posted by: gth802s Go to Quoted Post
It seems it is not just the issue with m[2]. The values calculated by that loop are not the correct ones; the correct values should be:

0.234,0.522,0.564,0.524,0.528,0.527,0.535,0.564,0.614,0.621


It would help showing your conclusive solving system

Offline gth802s  
#7 Posted : 22 December 2023 02:41:40(UTC)
gth802s


Rank: Member

Groups: Registered
Joined: 21/11/2023(UTC)
Posts: 24
United States

Was thanked: 2 time(s) in 2 post(s)
The attached worksheet containing a while loop works if I do it one at a time. I know it works because a) I have outputs from a commercial software package that does the same calculation, and Cool I can do it the brute force way by manually iterating one at a time :-)

While Loop.sm (17kb) downloaded 3 time(s).

Originally Posted by: Jean Giraud Go to Quoted Post
Originally Posted by: gth802s Go to Quoted Post
It seems it is not just the issue with m[2]. The values calculated by that loop are not the correct ones; the correct values should be:

0.234,0.522,0.564,0.524,0.528,0.527,0.535,0.564,0.614,0.621


It would help showing your conclusive solving system

Edited by user 22 December 2023 02:56:42(UTC)  | Reason: Not specified

Offline gth802s  
#8 Posted : 22 December 2023 03:37:59(UTC)
gth802s


Rank: Member

Groups: Registered
Joined: 21/11/2023(UTC)
Posts: 24
United States

Was thanked: 2 time(s) in 2 post(s)
I finally got it working :-) I had to break out my pencil and notepad and go through the first couple of iterations manually. Attached is the loop that works.

While Loop with Vectors.sm (19kb) downloaded 8 time(s).
Offline Jean Giraud  
#9 Posted : 22 December 2023 04:43:35(UTC)
Jean Giraud

Rank: Guest

Groups: Registered
Joined: 04/07/2015(UTC)
Posts: 6,866
Canada

Was thanked: 981 time(s) in 809 post(s)
Originally Posted by: gth802s Go to Quoted Post
Attached is the loop that works.

Next two things ... plot m, spline

plot m indexed x ... cinterp
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.