Matlab read specified lines and create a structure - string

Suppose we have a txt file containing the following data (output.txt):
MC Sim:
x1 = 28.87 x2 = 28.87 x3 = 24.02
C = 51 Iter = 1 S = 3.00 M= 26.77
C = 51 Iter = 2 S = 7.85 M= 32.70
C = 51 Iter = 3 S = 18.62 M= 46.85
MC Sim:
x1 = 28.87 x2 = 28.87 x3 = 19.65
C = 51 Iter= 1 S = 3.00 M = 22.28
C = 51 Iter= 2 S = 3.77 M = 25.10
C = 51 Iter= 3 S = 4.99 M = 28.89
C = 51 Iter= 4 S = 8.40 M = 35.78
C = 51 Iter= 5 S = 12.13 M = 46.22
C = 51 Iter= 6 S = 27.95 M = 72.74
I would like to read some lines and create a specified strucure from the values it contains, automatically. For example, suppose we want to a create the following structure using the values of S & M between lines 3 and 5:
Iter1.S = the value of S at line 3 (3.00)
Iter1.M = the value of M at line 3 (26.77)
Iter2.S = the value of S at line 4 (7.85)
Iter2.M = the value of M at line 4 (32.70)
Iter3.S = the value of S at line 5 (18.62)
Iter3.M = the value of M at line 5 (46.85)
Please pay attention the name of each field should be a concatenated string containing the variable name Iter and its value, e.g. Iter1 = Iter+1 .

Related

VBA results in #VALUE! only when For loop is used

I am writing a function with a For loop, and ultimately the value of the function will depend on the output of the For loop. For now as a test, the value of the function is a constant. If the For loop is in the code, the function results in #Value!. If I remove the For loop, the output is the specified constant value. How does the For loop need to be specified to avoid this? Good values for Tc and Th as a test would be 100 and 300, respectively.
Function kndT(material As Integer, Tc As Double, Th As Double) As Variant
Dim x As Double
Select Case material
Case 4
If Th > 300 Then
Tmax = 300
Else
Tmax = Th
End If
A = 0.07918
b = 1.0957
c = -0.07277
D = 0.08084
e = 0.02803
f = -0.09464
g = 0.04179
h = -0.00571
i = 0
End Select
hh = (Tmax - Tc) / 999
fi = 0
nc = 1
For i = 1 To 999
Temp = (Tc + i * hh)
x = Log(Temp) / Log(10#)
y = A + b * x + c * x ^ 2 + D * x ^ 3 + e * x ^ 4 + f * x ^ 5 + g * x ^ 6 + h * x ^ 7 + i * x ^ 8
fn = 10 ^ y
If nc = 3 Then
fi = fi + 2 * fn
nc = 1
Else
fi = fi + 3 * fn
nc = nc + 1
End If
Next i
kndT = 2
End Function

Non local keyword doesn't seems to be working

I expect the variables inside foo to change as I am using nonlocal keyword in the method bar. My understanding is that the variables a,b,c are no longer local in bar and they are just an assignment to the variables a,b,c in foo and therefore the values should change in foo.
I don't see that happening when I run the below code in python3. Wondering what am I missing
Below is the output
From foo
a = 10
b = 20
c = 30
From bar
a = 11
b = 12
c = 19
From chu
a = 200
b = 300
c = 500
Here is the code
def foo():
a = 10
b = 20
c = 30
print("From {func}\na = {a}\nb = {b}\nc = {c}\n".format(func="foo", a=a, b=b, c=c))
def bar():
nonlocal a
nonlocal b
nonlocal c
a = 11
b = 12
c = 19
print("From {func}\na = {a}\nb = {b}\nc = {c}\n".format(func="bar", a=a, b=b, c=c))
def chu():
a = 200
b = 300
c = 500
print("From {func}\na = {a}\nb = {b}\nc = {c}\n".format(func="chu",a=a,b=b,c=c))
chu()
bar()
foo()

How to assign multiple values to multiple variables in python (when looping occurs)

When using the following loop, how can I create a separate variable for each value generated (while looping occurs)?
Eg: with n = 1, m1 = 1; n = 2, m2 = 2; n = 3, m3 = 3; etc.
How can I print each variable generated (m1, m2, etc.) with it's assigned value as separate items?
for n in range(11):
print(n)
The following code demonstrates one approach that you could try taking:
>>> variables = {}
>>> for n in range(11):
variables[f'm{n}'] = n
>>> for key, value in variables.items():
print(f'{key} = {value}')
m0 = 0
m1 = 1
m2 = 2
m3 = 3
m4 = 4
m5 = 5
m6 = 6
m7 = 7
m8 = 8
m9 = 9
m10 = 10
>>>

Solving a line intercept equation

What is A and B so that the line Ay = Bx + 1 passes through points (1, 3) and (5,13) in the Cartesian plane?
I have been trying to solve it using the slope intercept equation to no avail. This is taken from Dale Hoffman's Contemprary Calculus.
First, I would reorder to get canonical form,
y = (B/A) * x + (1/A) = m * x + b
Now we find slope (m):
m = dy / dx = (13 - 3) / (5 - 1) = 2.5
sub in to find b:
3 = 2.5 * 1 + b
b = 0.5
Now sub back to find the values you want,
b = 0.5 = 1 / A
A = 2
m = 2.5 = B / 2
B = 5

While loop comparing two variables

I'm trying to find and print the minimum value for n that satisfies ca >= cb:
ap = 80000
bp = 200000
at = 1.03
bt = 1.015
n = 1
ca = ap*at*n
cb = bp*bt*n
while cb > ca:
n = n + 1
print(n)
The code just runs and prints n + 1 indefinitely, though. What is the correct approach to this problem?
ap = 80000
bp = 200000
at = 1.03
bt = 1.015
n = 1
ca = ap*at*n
cb = bp*bt*n
while cb > ca:
n += 1
ca = ap*at*n
cb = bp*bt*n
This won't converge though.

Resources