Python Forum
Need to fix SyntaxError in cycle try
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Need to fix SyntaxError in cycle try
#3
I transfer a solved physics problem to python. Its task is to solve a system of differential equations with conditions (the Cauchy problem) in a cycle for different vectors of initial values (y0 in solve_ivp). After this, I have to define the optimal solution among these obtained - i.e. the one that satisfies the optimality condition.

What I am doing now: I applied basics conditions for variables and I have collected all possible variables' values for t0 - i.e. a set of values for each variable (these combinations are a set of y0 vectors that can be provided in solve_ivp, but these are many that aren't solution). Now, in order to form vectors y0 that will be provided to solve_ivp, I need to apply the condition that ht is equal to zero (this is the main condition of the problem) and to collect (in list format) only these combinations of elements that satisfy the condition. There are indeed combinations of elements that will ensure that ht is equal to zero (with a given accuracy).

I have the following difficulty: each set of variable' values is a list or an array and I have to iterate each variable through the elements i.e. through the values. Otherwise, the function try to accept lists or arrays instead of one value and this leads to an error. So I made this mistake with function definition.

My updated code is below, it seems to work, but takes a really long time.

def function_gam(et, y3, phb, kj, y7):
  ht = kj[0] * et * cos(y3) / sqrt(phb) + kj[1] * -et0 * sin(y3) / sqrt(phb) + y7 * sqrt(1 / phb) * et0 * sin(y3) + funct_labt(y3, kj[1], y7, kj[0]) * (sqrt(1 / phb) * (1 + et * cos(y3) * (1 + et * cos(y3)**2) / phb**2))
  return ht

for i in z30: # iteratation through values for variable z30
  for o in zres: # iteratation through values for array zres which includes 2 interconnected variales. The values of the first are in zres[0], for the other - in zres[1]
    for l in z70: # iteratation through values for variable z70
      ht0 = function_gam(et0, i, ph0b, o, l)
      if isclose(ht0, 0, abs_tol=1.0e-9): # only these combinations of variables values are collected that satisfy the condition ht == 0
        lh0 = i, o[0], o[1], l
        print(lh0)
Reply


Messages In This Thread
RE: Need to fix SyntaxError in cycle try - by alexfrol86 - Mar-24-2022, 09:05 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing a cycle to find the nearest point from the array Tysrusko 0 229 May-10-2024, 11:49 AM
Last Post: Tysrusko
  Trying to cycle through a list of charcters pooky2483 12 4,699 Sep-28-2020, 06:55 AM
Last Post: pooky2483
  <while> cycle is not interrupted when using the <random>module ShityCoder 3 2,270 Sep-04-2020, 04:05 PM
Last Post: ShityCoder
  Cycle of numpy variables Zero01 0 1,600 Jul-31-2020, 11:58 AM
Last Post: Zero01
  stop cycle while windows11 1 2,095 May-16-2020, 03:17 PM
Last Post: deanhystad
  Occurrences using FOR and IF cycle P86 2 2,605 Jul-29-2019, 04:37 PM
Last Post: ThomasL
  pool map cycle skorost5 5 3,944 Apr-07-2019, 09:21 AM
Last Post: skorost5
  lifetime cycle counter Ixxxxxx 0 2,540 Mar-07-2018, 07:26 PM
Last Post: Ixxxxxx

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020