how to solve subscript out of range in qbasic - qbasic

I am in big problem in solving a code essential for me, and I need the solution as soon as possible
in fact, I have little knowledge of programming in basic
I have a problem with this code.
I have an equation, and I use this code to solve this equation
when I run the program
this error appears
subscript out of range
is there any solution to this problem
0 Print "******** impact *******"
20 Print "____________"
30 Print "this programs is used to solve impact integral"
40 Print "equation of simply supported slab to "
50 Print "optain the following"
60 Print " (1) force _time history"
70 Print " (2) central deflection - time history"
80 Print "-----------------"
90 Print "input data:"
100 Print " (1) FUNDAMENTAL NATURAL FREQUANCY (RAD/SEC)--- W1"
110 Print " (2) STRIKER MASS (KG.) ----- Mst"
120 Print " (3) MASS OF SLAB (KG)---- Ms"
130 Print " (4) hertz constant (n/m^1.5)----k"
140 Print " (5) STRICKER VELOCITY (M/S)----Vo"
150 Print " (6) NUMBER OF MODES----N"
160 Print "_________"
170 Input " W11, MST, MS, K, VO, N", W11, MST, MS, K, VO, N
180 Print "W1="; W11; "RAD/SEC"
190 Print "MST="; MST; "KG"
200 Print "MS="; MS; "KG"
210
220 Print " K = "; K; "N/M^1.5"
230 Print STANDARD
240 Print "VO="; VO; "M/S"
241 Print " N = "; N
250 Print
260 Print
270 K1 = K
280 V = VO
290 W1 = W11 / 2
300 TINF = 2.94 * (MST / (.8 * K1 * V ^ .5)) ^ .4 * 1000
310 DT = TINF / 10
320 M = 20
330 DT = PROUND(DT, 0)
340 DT = DT / 1000
350 M = 20
360 Option Base 1
370 Dim W(11, 11), Z(11, 11), F(30), D(30), BM(30), SH(30), A(30), T(30), DF(30), S(11, 11, 30), C(11, 11, 30)
380 ReDim W(N, N), Z(N, N)
390 For I = 1 To N Step 2
400 For K = 1 To N Step 2
410 W(K, I) = W1 * (I ^ 2 + K ^ 2)
420 Z(K, I) = W(K, I) * DT
430 Next K
440 Next I
450 ReDim F(M), D(M), A(M), T(M), DF(M), BM(M), SH(M), S(N, N, M), C(N, N, M)
460 F(1) = D(1) = A(1) = T(1) = 0
470 For I = 1 To N Step 2
480 For K = 1 To N Step 2
490 S(K, I, 1) = C(K, I, 1) = 0
500 Next K
510 Next I
520 B1 = 0
530 For I = 1 To N Step 2
540 For K = 1 To N Step 2
550 B1 = B1 + (1 - Sin(Z(K, I)) / Z(K, I)) / W(K, I) ^ 2
560 Next K
570 Next I
580 B = -DT ^ 2 / (6 * MST) - 4 * B1 / MS
590 VE = 0
600 For I = 2 To M
610 T(I) = (I - 1) * DT
620 GM = 0
630 If VE = 1 Then 970
640 For J = 2 To I
650 GM = GM + F(J - 1)
660 Next J
670 AA = 0
680 For J = 1 To N Step 2
690 For K = 1 To N Step 2
700 FF = F(I - 1) * (Sin(Z(K, J)) / Z(K, J) - Cos(Z(K, J))) / W(K, J)
710 AA = AA + 4 * (Cos(Z(K, J)) * S(K, J, I - 1) + Sin(Z(K, J)) * C(K, J, I - 1) + FF) / (MS * W(K, J))
720 Next K
730 Next J
740 A(I - 1) = V * (I - 1) * DT - (D(I - 1) + DT ^ 2 * (GM - F(I - 1) / 6)) / MST - AA
750 F = F(I - 1)
760 If A(I - 1) + B * F < 0 Then 840
770 F1 = (A(I - 1) + B * F) ^ 1.5 * K1
780 X = Abs(F1 - F)
790 If X < 10 Then 820
800 F = F1
810 GoTo 770
820 F(I) = F1
830 GoTo 850
840 F(I) = 0
850 D(I) = D(I - 1) + DT ^ 2 * (GM + (F(I) - F(I - 1)) / 6)
860 For J = 1 To N Step 2
870 For K = 1 To N Step 2
880 S(K, J, I) = Cos(Z(K, J)) * S(K, J, I - 1) + Sin(Z(K, J)) * C(K, J, I - 1) + (1 - Sin(Z(K, J)) / Z(K, J)) * (F(I) - F(I - 1)) / W(K, J) + (1 - Cos(Z(K, J))) * F(I - 1) / W(K, J)
890 C(K, J, I) = Cos(Z(K, J)) * C(K, J, I - 1) - Sin(Z(K, J)) * S(K, J, I - 1) + (1 - Cos(Z(K, J))) / Z(K, J) * (F(I) - F(I - 1)) / W(K, J) + Sin(Z(K, J)) * F(I - 1) / W(K, J)
900 Next K
910 Next J
920 DF = 0
930 For J = 1 To N Step 2
940 For K = 1 To N Step 2
950 DF = DF + 4 * S(K, J, I) / W(K, J) / MS
960 Next K
970 Next J
980 DF(I) = DF
990 If F(I) = 0 Then 1010
1000 Next I
1010 Print "----------------------------------------------------------"
1020 Print "{TIME (MS)},{FORCE (KN)},{DEFLECTION(MM)}"
1030 Print "----------------------------------------------------------"
1040 II = I
1050 For O = 1 To II
1060
1070 Print Tab(1); ":"; Tab(5); T(0) * 1000; Tab(18); ":"; Tab(22); F(O) / 1000; Tab(34); ":"; Tab(42); DF(O) * 1000; Tab(56); ":"
1080 Print "-----------------------------------------------------------"
1090 Next O
1100 End

Related

"fsolve" doesn't work for system nonlinear equation

I have these equations:
syms pm pr teta s
A1 = -2 * b1 * pm + 2 * b2 * pr + b * teta + (1-t) * s + (1-p) * a + c * (b1 - b2);
A2 = 2 * b2 * pm + 2 * b1 * pr + (1-b) * teta + t * s + p * a + c * (b1 - b2);
A3 = b * pm + (1-b) * pr - n * teta - c;
A4 = (1-t) * pm + t * pr - k * s - c;
eqns = [A1,A2,A3,A4];
F=#(pm, pr, teta, s) [A1
A2
A3
A4];
x0 = [10, 10, 10, 10];
fsolve(F, x0)
How I can solve them?
(When I use fsolve, it shows this error: FSOLVE requires all values returned by functions to be of data type double)
Since you tagged Mathematica
A1 = -2*b1*pm + 2*b2*pr + b*teta + (1 - t)*s + (1 - p)*a + c*(b1 - b2);
A2 = 2*b2*pm + 2*b1*pr + (1 - b)*teta + t*s + p*a + c*(b1 - b2);
A3 = b*pm + (1 - b)*pr - n*teta - c;
A4 = (1 - t)*pm + t*pr - k*s - c;
FullSimplify[Solve[{A1 == 10, A2 == 10, A3 == 10, A4 == 10}, {pm, pr, teta, s}]]
pm -> ((k ((-1 + b)^2 + 2 b1 n) + n t^2) (b (10 + c) k +
n (10 + c + 10 k - a k - b1 c k + b2 c k +
a k p - (10 + c) t)) + ((-1 + b) (10 + c) k +
k n (-10 + b1 c - b2 c + a p) - (10 + c) n t) (b k - b^2 k +
n (2 b2 k + t - t^2)))/(k n (1 - 2 b1 k + b^2 (1 + 4 b2 k) +
2 (b1 - 2 (b1^2 + b2^2) k) n - 2 t -
4 (b1 + b2) n t + (1 + 4 b2 n) t^2 +
2 b (-1 + 2 b1 k - 2 b2 k + t)))
pr -> (c +
b^2 (10 + c - (-20 + a + 2 b1 c - 2 b2 c) k) + 2 b1^2 c k n -
b2 (20 + c - 2 (-10 + a) k + 2 b2 c k) n - a (1 + 2 b2 k) n p -
2 b1 k (10 + c + n (10 - a p)) + 10 (1 + n - 2 t) -
c (2 + b2 n) t +
n (-30 + a + 20 b2 + a p) t + (10 + c - (-20 + a) n +
2 b2 c n) t^2 - b1 n (c + 20 t + c t (-1 + 2 t)) +
b (k (-10 + a + 20 b1 - 20 b2 - a p) + 20 (-1 + t) +
c (-2 + 3 b1 k - 3 b2 k + 2 t)))/(1 - 2 b1 k +
b^2 (1 + 4 b2 k) + 2 (b1 - 2 (b1^2 + b2^2) k) n - 2 t -
4 (b1 + b2) n t + (1 + 4 b2 n) t^2 +
2 b (-1 + 2 b1 k - 2 b2 k + t))
teta -> (10 - 20 b2 - b2 c -
20 b2 k + 2 a b2 k + 40 b2^2 k + 2 b2^2 c k +
2 b1^2 (20 + 3 c) k - a p -
2 a b2 k p + (-30 + 3 b2 (20 + c) + a (1 + p)) t - (-20 + a +
2 b2 (20 + c)) t^2 +
b (-10 - 4 b1^2 c k + a p +
b1 (20 + 40 k - 2 a k + c (3 + 4 b2 k - 2 t)) + 20 t - a t +
b2 (20 + c - 2 a k + 4 a k p - 2 (20 + c) t)) +
b1 (2 k (-10 + a p) + 20 (-1 + t) + c (-3 + (5 - 2 t) t)))/(1 -
2 b1 k + b^2 (1 + 4 b2 k) + 2 (b1 - 2 (b1^2 + b2^2) k) n - 2 t -
4 (b1 + b2) n t + (1 + 4 b2 n) t^2 +
2 b (-1 + 2 b1 k - 2 b2 k + t))
s -> (20 b1 + b1 c - b2 c -
2 b^2 (-10 + b1 c + b2 (20 + c)) + 20 b1 n + 40 b1^2 n -
20 b2 n + 40 b2^2 n + 2 b1^2 c n + 4 b1 b2 c n +
2 b2^2 c n - (b1 - b2) (20 + c) t +
4 b1 (-10 + b1 c - b2 c) n t - 10 (-1 + 2 b2 + t) +
a (-1 - b^2 - 2 b1 n + p + 2 (b1 + b2) n p + t - p t +
2 n (b1 + b2 - 2 b2 p) t - b (-2 + p + t)) +
b (-(-10 + b2 (20 + c)) (-3 + 2 t) + b1 (-20 + c - 2 c t)))/(1 -
2 b1 k + b^2 (1 + 4 b2 k) + 2 (b1 - 2 (b1^2 + b2^2) k) n - 2 t -
4 (b1 + b2) n t + (1 + 4 b2 n) t^2 +
2 b (-1 + 2 b1 k - 2 b2 k + t))

How to create a matrix in Matlab with every entry being the output of a bivariate function

I want to create a 4 x 4 matrix with each entry representing f(x,y) where both x and y take values 0, 1, 2 and 3. So the first entry would be f(0,0), all the way to f(3,3).
The function f(x,y) is:
3 * cos(0*x + 0*y) + 2 * cos(0*x + 1*y) + 3 * cos(0*x + 2*y) + 8 * cos(0*x + 3*y)
+ 3 * cos(1*x + 0*y) + 25 * cos(1*x + 1*y) + 3 * cos(1*x + 2*y)
+ 8 * cos(1*x + 3*y)
+ 3 * cos(2*x + 0*y) + 25 * cos(2*x + 1*y) + 3 * cos(2*x + 2*y)
+ 8 * cos(2*x + 3*y)
+ 3 * cos(3*x + 0*y) + 25 * cos(3*x + 1*y) + 3 * cos(3*x + 2*y)
- 90 * cos(3*x + 3*y)
I haven't used Matlab much, and it's been a while. I have tried turning f(x,y) into a #f(x,y) function; using the .* operator; meshing x and y, etc. All of it without success...
Not sure, what you've tried exactly, but using meshgrid is the correct idea.
% Function defintion (abbreviated)
f = #(x, y) 3 * cos(0*x + 0*y) + 2 * cos(0*x + 1*y) + 3 * cos(0*x + 2*y)
% Set up x and y values.
x = 0:3
y = 0:3
% Generate grid.
[X, Y] = meshgrid(x, y);
% Rseult matrix.
res = f(X, Y)
Generated output:
f =
#(x, y) 3 * cos (0 * x + 0 * y) + 2 * cos (0 * x + 1 * y) + 3 * cos (0 * x + 2 * y)
x =
0 1 2 3
y =
0 1 2 3
res =
8.00000 8.00000 8.00000 8.00000
2.83216 2.83216 2.83216 2.83216
0.20678 0.20678 0.20678 0.20678
3.90053 3.90053 3.90053 3.90053

Scala tail recursive method has an divide and remainder error

I'm currently computing the binomial coefficient of two natural numbers by write a tail recursion in Scala. But my code has something wrong with the dividing numbers, integer division by k like I did as that will give you a non-zero remainder and hence introduce rounding errors. So could anyone help me figure it out, how to fix it ?
def binom(n: Int, k: Int): Int = {
require(0 <= k && k <= n)
def binomtail(n: Int, k: Int, ac: Int): Int = {
if (n == k || k == 0) ac
else binomtail(n - 1, k - 1, (n*ac)/k)
}
binomtail(n,k,1)
}
In general, it holds:
binom(n, k) = if (k == 0 || k == n) 1 else binom(n - 1, k - 1) * n / k
If you want to compute it in linear time, then you have to make sure that each intermediate result is an integer. Now,
binom(n - k + 1, 1)
is certainly an integer (it's just n - k + 1). Starting with this number, and incrementing both arguments by one, you can arrive at binom(n, k) with the following intermediate steps:
binom(n - k + 1, 1)
binom(n - k + 2, 2)
...
binom(n - 2, k - 2)
binom(n - 1, k - 1)
binom(n, k)
It means that you have to "accumulate" in the right order, from 1 up to k, not from k down to 1 - then it is guaranteed that all intermediate results correspond to actual binomial coefficients, and are therefore integers (not fractions). Here is what it looks like as tail-recursive function:
def binom(n: Int, k: Int): Int = {
require(0 <= k && k <= n)
#annotation.tailrec
def binomtail(nIter: Int, kIter: Int, ac: Int): Int = {
if (kIter > k) ac
else binomtail(nIter + 1, kIter + 1, (nIter * ac) / kIter)
}
if (k == 0 || k == n) 1
else binomtail(n - k + 1, 1, 1)
}
Little visual test:
val n = 12
for (i <- 0 to n) {
print(" " * ((n - i) * 2))
for (j <- 0 to i) {
printf(" %3d", binom(i, j))
}
println()
}
prints:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1
1 12 66 220 495 792 924 792 495 220 66 12 1
Looks ok, compare it with this, if you want.
Andrey Tyukin's excellent example will fail with larger n, say binom(10000, 2), but can be easily adapted to use BigInt.
def binom(n: Int, k: Int): BigInt = {
require(0 <= k && k <= n)
#annotation.tailrec
def binomtail(nIter: Int, kIter: Int, ac: BigInt): BigInt = {
if (kIter > k) ac
else binomtail(nIter + 1, kIter + 1, (nIter * ac) / kIter)
}
if (k == 0 || k == n) 1
else binomtail(n - k + 1, 1, BigInt(1))
}

How to find solution of non-linear algebraic equations using MATLAB?

I have 10 non-linear equation,
L1 + 1.3*3*(P1^0.3) + 2*P1 = 12
L2 + 1.2*5*(P2^0.2) + 3*P2 = 20
L3 + 1.15*6*(P3^0.15) + 5*P3 = 28
L4 - L1*0.9*0.4*(X1^-0.1) = 0
L4 - L2*0.8*0.5*(X2^-0.2) = 0
L4 - L3*0.7*0.6*(X3^-0.3) = 0
P1 - 0.4*(X1^0.9) = 0
P2 - 0.5*(X2^0.8) = 0
P3 - 0.6*(X3^0.7) = 0
X1 + X2 + X3 = 10
I do not have any initial guesses for the solutions, however, all variables are essentially non-negative i.e.,
L1>0, L2>0, L3>0, L4>0, P1>0, P2>0, P3>0, X1>0, X2>0, X3>0
I am trying to solve these equations by executing following command,
clear
clc
syms L1 L2 L3 L4 P1 P2 P3 X1 X2 X3
sol=solve([ L1 + 1.3*3*(P1^0.3) + 2*P1 == 12, L2 + 1.2*5*(P2^0.2) + 3*P2 == 20, L3 + 1.15*6*(P3^0.15) + 5*P3 == 28, ...
L4 - L1*0.9*0.4*(X1^-0.1) == 0, L4 - L2*0.8*0.5*(X2^-0.2) == 0, L4 - L3*0.7*0.6*(X3^-0.3) == 0, ...
P1 - 0.4*(X1^0.9) == 0, P2 - 0.5*(X2^0.8) == 0, P3 - 0.6*(X3^0.7) ==0, X1+X2 +X3 == 10, ...
L1>0, L2>0, L3>0, L4>0, P1>0, P2>0, P3>0, X1>0, X2>0, X3>0], [L1, L2, L3, L4, P1, P2, P3, X1, X2, X3]);
But it shows error as
Warning: 32 equations in 11 variables.
> In C:\Program Files\MATLAB\R2013a\toolbox\symbolic\symbolic\symengine.p>symengine at 56
In mupadengine.mupadengine>mupadengine.evalin at 97
In mupadengine.mupadengine>mupadengine.feval at 150
In solve at 170
Warning: Explicit solution could not be found.
> In solve at 179
>>
How to solve these nonlinear equations?
UPDATE: DIFF was actually the difference that would be a number, say
10, 20 or 30. I have removed DIFF from here.
You have 10 equations, but you have 10 variables.
You can help by hand.
Reduce linear equation:
P1 = 0.4(x^(0.9))
P2 = 0.5(x^(0.8))
P3 = 0.6(x^(0.7))
and
L4 = 0.36L1*X1^(-0.1) = 0.4L2*X2^(-0.2) = 0.42L3*X3^(-0.3)
then we get 4 equations on 4 variables:
L1 + 3.9*((0.4^(0.3))*X1^(0.27)) + 0.8*X1^(0.9) - 12 = 0
0.9*(X1^(-0.1))*(X2^(0.2))*L1 + 6*((0.5^0.2)*(x2^(0.16))) + 1.5*X2^(0.8) - 20 = 0
(6/7)*(X1^(-0.1))*(X3^(0.3))*L1 + 6.9*((0.6^0.15)*(X3^(0.105))) + 3*(x3^0.7) - 28 = 0
x1 + X2 + X3 = 10
and try to solve
sol=solve([X1+X2+X3 == 10,L1 + 3.9*((0.4^(0.3))*X1^(0.27)) + 0.8*X1^(0.9) - 12 == 0, 0.9*(X1^(-0.1))*(X2^(0.2))*L1 + 6*((0.5^0.2)*(X2^(0.16))) + 1.5*X2^(0.8) - 20 == 0,(6/7)*(X1^(-0.1))*(X3^(0.3))*L1 + 6.9*((0.6^0.15)*(X3^(0.105))) + 3*(X3^0.7) - 28 == 0, X1 > 0, X2 > 0, X3 > 0, L1 > 0]);
I try to solve it, but I out of memory.
It want to smarter method.

Octal number equations

So I'm reading a book on how binary bits are converted into octal numbers.
When trying to explain the concept, they give this equation
N= S(...((d8)2^8+(d7)2^7+(d6)2^6)+((d5)2^5+(d4)2^4+(d3)2^3)+((d2)2^2+(d1)2^1+d0))
or
N= S(...((d8)2^2 +(d7)2+(d6))2^6 + ((d5)2^2 +(d4)2^1 + (d3))2^3 + ((d2)2^2+(d1)2^1+d0))
d represents the digit found within the bit, e.g. if the least significant bit was 1, then (d0) would be 1.
I understand all of this, but they elaborate further saying that the parenthesized expressions ((d8)2^2 +(d7)2+(d6)) are coefficients of base 8 digits, N=S((d2)8^2+(d1)*8+(d0)).
Can someone explain what they mean by the parenthesized expressions being coefficients of base8 digits?
The digits di are the binary digits of the number. We can compute the number from its binary digits like this:
n = ∑ i 2i di = 20 d0 + 21 d1 + 22 d2 + ⋯
(This is in fact what defines “binary”, if we add the condition that the digits are integers and 0 ≤ di < 2 for all i.)
Suppose we name the octal digits of the number oj. We can compute the number from its octal digits like this:
n = ∑ j 8j oj = 80 o0 + 81 o1 + 82 o2 + ⋯
(This is what defines “octal”, if we add the condition that the digits are integers and 0 ≤ oj < 8 for all j.)
Now let's look back at the binary equation. The first step is the trickiest. We will change the way the subscript is used so that each term of the summation uses three binary digits:
n = ∑ j 23 j + 0 d3 j + 0 + 23 j + 1 d3 j + 1 + 23 j + 2 d3 j + 2
Convince yourself that that equation computes the same n as the first equation I gave.
I assume you know that xa + b = xa xb. So we can separate those 23 j + b coefficients like this:
n = ∑ j (23 j 20) d3 j + 0 + (23 j 21) d3 j + 1 + (23 j 22) d3 j + 2
Then we can factor out the 23 j term like this:
n = ∑ j 23 j (20 d3 j + 0 + 21 d3 j + 1 + 22 d3 j + 2)
I assume you also know that xa b = (xa)b. So we can split the 23 j term like this:
n = ∑ j (23)j (20 d3 j + 0 + 21 d3 j + 1 + 22 d3 j + 2)
And we can simplify 23 to 8:
n = ∑ j 8j (20 d3 j + 0 + 21 d3 j + 1 + 22 d3 j + 2)
Compare this to the formula for computing the number from its octal digits, which I repeat here:
n = ∑ j 8j oj
So we can conclude this:
oj = 20 d3 j + 0 + 21 d3 j + 1 + 22 d3 j + 2
For example, let's take j = 2:
o2 = 20 d3×2 + 0 + 21 d3×2 + 1 + 22 d3×2 + 2 = 20 d6 + 21 d7 + 22 d8