While loop question - working with vectors

While loop question - working with vectors - Сообщения

#1 Опубликовано: 20.12.2023 09:40:40
Fikret Atalay

Fikret Atalay

2 сообщений из 24 понравились пользователям.

Группа: User

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 (14 КиБ) скачан 32 раз(а).
#2 Опубликовано: 20.12.2023 12:18:08
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 сообщений из 1674 понравились пользователям.

Группа: User

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 (15 КиБ) скачан 33 раз(а).

Best regards.
Alvaro.
#3 Опубликовано: 20.12.2023 21:04:57
Fikret Atalay

Fikret Atalay

2 сообщений из 24 понравились пользователям.

Группа: User

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.
#4 Опубликовано: 21.12.2023 01:03:41
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 сообщений из 1674 понравились пользователям.

Группа: User

Wrote

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.

Wrote

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.
#5 Опубликовано: 21.12.2023 04:28:16
Fikret Atalay

Fikret Atalay

2 сообщений из 24 понравились пользователям.

Группа: User

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

Wrote



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.

#6 Опубликовано: 21.12.2023 19:05:35
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: User

Wrote

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

#7 Опубликовано: 21.12.2023 20:41:40
Fikret Atalay

Fikret Atalay

2 сообщений из 24 понравились пользователям.

Группа: User

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 b) I can do it the brute force way by manually iterating one at a time :-)

While Loop.sm (16 КиБ) скачан 26 раз(а).

Wrote

Wrote

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

#8 Опубликовано: 21.12.2023 21:37:59
Fikret Atalay

Fikret Atalay

2 сообщений из 24 понравились пользователям.

Группа: User

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 (18 КиБ) скачан 33 раз(а).
#9 Опубликовано: 21.12.2023 22:43:35
Jean Giraud

Jean Giraud

983 сообщений из 6866 понравились пользователям.

Группа: User

Wrote

Attached is the loop that works.


Next two things ... plot m, spline

plot m indexed x ... cinterp
  • Новые сообщения Новые сообщения
  • Нет новых сообщений Нет новых сообщений