how to solve simultaneous equations with solve and with result without roots - matlab

Here is the code:
syms G1 G2 G3 M1 M2 M3 P1 P2 P3 D1 D2 D3
S = solve(0 == 0.9 * (20 - G1) - 0.012 * D3 * G1, ...
0 == 0.9 * (20 - G2) - 0.012 * D1 * G2, ...
0 == 0.9 * (20 - G3) - 0.012 * D2 * G3, ...
0 == -0.0033 * M1 + 0.002 * G1, ...
0 == -0.0033 * M2 + 0.002 * G2, ...
0 == -0.0033 * M3 + 0.002 * G3, ...
0 == 0.1 * M1 - 0.0033 * P1 + 2 * 0.5 * D1 - 2 * 0.025 * (P1 ^ 2), ...
0 == 0.1 * M2 - 0.0033 * P2 + 2 * 0.5 * D2 - 2 * 0.025 * (P2 ^ 2), ...
0 == 0.1 * M3 - 0.0033 * P3 + 2 * 0.5 * D3 - 2 * 0.025 * (P3 ^ 2), ...
0 == -0.5 * D1 + 0.025 * (P1 ^ 2) + 0.9 * (20 - G2) - 0.012 * D1 * G2, ...
0 == -0.5 * D2 + 0.025 * (P2 ^ 2) + 0.9 * (20 - G3) - 0.012 * D2 * G3, ...
0 == -0.5 * D3 + 0.025 * (P3 ^ 2) + 0.9 * (20 - G1) - 0.012 * D3 * G1)
The problem is that I need real solution, but I have got answers with roots. How can I get real answers?

Assuming by "real solution" you mean a numeric value instead of the exact root-of-polynomial form, there are two options depending on how you intend on using the results:
double: this will evaluate the RootOf expressions, assuming there are no free parameters, and return a numeric output of class double. While the output is now limited to double-precision, if your goal is to use the roots in computation and care about performance, this is the fastest option.
vpa: this will evaluate the RootOf expressions, assuming there are no free parameters, and return a Symbolic output of class sym. While the output is now able to be approximated to varying degrees of accuracy at evaluation by calling digits beforehand, there may be a large computational overhead in subsequent calculations due to its Symbolic nature.

Related

solve complex equations in MATLAB (Variable Equations)

I have some equations which include some parameters and variables.
Each variable defines and also exists in other equations. these are my equations:
syms Pof s Pon teta landa PIon PIof PISC
Pof = (s * y - teta + T * teta - landa + Pi * landa + alpha * p + Pon * b1 + w * b2)/(2 * b2);
s = -(y * (w - Pof))/q;
Pon = (q * s * y + 2 * w * y - 2 * w * y^2 + 2 * q * alpha + 2 * q * Pi * landa - q * teta + 3 * q * T * teta - q * landa + q * Pi * landa - q * alpha * p - 2 * y * Pof + 2 * y^2 * Pof + C * q * b1 + q * w * b2)/(2 * q * b1);
teta = (C - 3 * C * T - Pon + 3 * T * Pon)/2 * q;
landa = (-1/2) * (-1 + Pi) * (C - Pon);
Don = (1-p) * alpha - b1 * Pon + b2 * Pof - (1-y) * s + T * teta + landa * Pi;
Dof = p * alpha - b1 * Pof + b2 * Pon + y * s - (1-T) * teta - landa * (1-Pi);
PIon = (Pon-C) * Don - (1/2) * n * teta^2 - (1/2) * q * landa^2;
PIof = (Pof-w) * Dof - (1/2) * L * s^2;
PISC = PIon + PIof;
How I can solve these in order to get a numeric answer for each variable?
(I don't want parametric answers)
The equations as stated are
which can be arranged as a linear system A x = b as follows
which you solve in Matlab as x = A \ b
On further investigation, it seems A is singular since it's 10×8 in size and cannot be inverted. So a least-squares solution is needed where
x = inv(AT* A)* AT b

how to solve subscript out of range in 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

Solve special system of linear equations in Matlab (GNU Octave)

I have a matrix, let's say 5x5 looking like this:
0 0 0 1 0
0 0 0 4/5 1/5
3/5 1/5 1/5 0 0
1/5 2/5 1/5 1/5 0
1/10 1/10 2/5 1/5 1/5
I need it to solve it like a system of linear equations looking like this (I can transpose it myself, but then multiplying it with the symbolic variables gets me into troubles):
0 * a + 0 * b + 3/5 * c + 1/5 * d + 1/10 + e = a
0 * a + 0 * b + 1/5 * c + 2/5 * d + 1/10 + e = b
0 * a + 0 * b + 1/5 * c + 1/5 * d + 2/5 + e = c
1 * a + 4/5 * b + 0 * c + 1/5 * d + 1/5 + e = d
0 * a + 1/5 * b + 0 * c + 0 * d + 1/5 + e = e
a + b + c + d + e = 1
I can easily solve this in wxMaxima, but I have to manually write all the values there, which is increasingly tedious with bigger matrices.
Is there a way to get the results after some steps using matlab operator \ for solving system of linear equations?
You can solve the equation set no?
>>[A-eye(5);ones(1,5)]\[0,0,0,0,0,1]'
ans =
0.1729
0.2061
0.1345
0.4350
0.0515
>> sum(ans)
ans =
1.0000
And a symbolic solution:
M=sym(A);
v=sym('[a;b;c;d;e]');
sol=solve(M*v==v,sum(v)==1);
returns solutions in the form sol.a, sol.b, ...

XOR with Neural Networks (Matlab)

So, I'm hoping this is a real dumb thing I'm doing, and there's an easy answer. I'm trying to train a 2x3x1 neural network to do the XOR problem. It wasn't working, so I decided to dig in to see what was happening. Finally, I decided to assign the weights my self. This was the weight vector I came up with:
theta1 = [11 0 -5; 0 12 -7;18 17 -20];
theta2 = [14 13 -28 -6];
(In Matlab notation). I deliberately tried to make no two weights be the same (barring the zeros)
And, my code, really simple in matlab is
function layer2 = xornn(iters)
if nargin < 1
iters = 50
end
function s = sigmoid(X)
s = 1.0 ./ (1.0 + exp(-X));
end
T = [0 1 1 0];
X = [0 0 1 1; 0 1 0 1; 1 1 1 1];
theta1 = [11 0 -5; 0 12 -7;18 17 -20];
theta2 = [14 13 -28 -6];
for i = [1:iters]
layer1 = [sigmoid(theta1 * X); 1 1 1 1];
layer2 = sigmoid(theta2 * layer1)
delta2 = T - layer2;
delta1 = layer1 .* (1-layer1) .* (theta2' * delta2);
% remove the bias from delta 1. There's no real point in a delta on the bias.
delta1 = delta1(1:3,:);
theta2d = delta2 * layer1';
theta1d = delta1 * X';
theta1 = theta1 - 0.1 * theta1d;
theta2 = theta2 - 0.1 * theta2d;
end
end
I believe that's right. I tested various parameters (of the thetas) with the finite differences method to see if they were right, and they seemed to be.
But, when I run it, it eventually just all boils down to returning all zeros. If I do xornn(1) (for 1 iteration) I get
0.0027 0.9966 0.9904 0.0008
But, if I do xornn(35)
0.0026 0.9949 0.9572 0.0007
(It's started a descent in the wrong direction) and by the time I get to xornn(45) I get
0.0018 0.0975 0.0000 0.0003
If I run it for 10,000 iterations, it just returns all 0's.
What is going on? Must I add regularization? I would have thought such a simple network wouldn't need it. But, regardless, why does it move away from an obvious good solution that I have hand fed it?
Thanks!
AAARRGGHHH! The solution was simply a matter of changing
theta1 = theta1 - 0.1 * theta1d;
theta2 = theta2 - 0.1 * theta2d;
to
theta1 = theta1 + 0.1 * theta1d;
theta2 = theta2 + 0.1 * theta2d;
sigh
Now tho, I need to figure out how I'm computing the negative derivative somehow when what I thought I was computing was the ... Never mind. I'll post here anyway, just in case it helps someone else.
So, z = is the sum of inputs to the sigmoid, and y is the output of the sigmoid.
C = -(T * Log[y] + (1-T) * Log[(1-y))
dC/dy = -((T/y) - (1-T)/(1-y))
= -((T(1-y)-y(1-T))/(y(1-y)))
= -((T-Ty-y+Ty)/(y(1-y)))
= -((T-y)/(y(1-y)))
= ((y-T)/(y(1-y))) # This is the source of all my woes.
dy/dz = y(1-y)
dC/dz = ((y-T)/(y(1-y))) * y(1-y)
= (y-T)
So, the problem, is that I accidentally was computing T-y, because I forgot about the negative sign in front of the cost function. Then, I was subtracting what I thought was the gradient, but was in fact the negative gradient. And, there. That's the problem.
Once I did that:
function layer2 = xornn(iters)
if nargin < 1
iters = 50
end
function s = sigmoid(X)
s = 1.0 ./ (1.0 + exp(-X));
end
T = [0 1 1 0];
X = [0 0 1 1; 0 1 0 1; 1 1 1 1];
theta1 = [11 0 -5; 0 12 -7;18 17 -20];
theta2 = [14 13 -28 -6];
for i = [1:iters]
layer1 = [sigmoid(theta1 * X); 1 1 1 1];
layer2 = sigmoid(theta2 * layer1)
delta2 = T - layer2;
delta1 = layer1 .* (1-layer1) .* (theta2' * delta2);
% remove the bias from delta 1. There's no real point in a delta on the bias.
delta1 = delta1(1:3,:);
theta2d = delta2 * layer1';
theta1d = delta1 * X';
theta1 = theta1 + 0.1 * theta1d;
theta2 = theta2 + 0.1 * theta2d;
end
end
xornn(50) returns 0.0028 0.9972 0.9948 0.0009 and
xornn(10000) returns 0.0016 0.9989 0.9993 0.0005
Phew! Maybe this will help someone else in debugging their version..

Matlab Vectorization : How to avoid this "for" loop?

I have following matrices :
X=1 2 3
Y=4 5 6
A=1 2 3
4 5 6
7 8 9
I Want to do
for each (i,j) in A
v = A(i,j)*X - Y
B(i,j) = v * v'
i.e. each element of A is multiplied by vector X, then resultant vector subtracts Y from itself and finally we take inner product of that vector to bring a single number.
Can it be done without for loop ?
One thing often forgotten in Matlab: The operator ' takes the conjugate transposed (.' is the ordinary transposed). In other words, A' == conj(trans(A)), whereas A.' == trans(A), which makes a difference if A is a complex matrix.
Ok, let's apply some mathematics to your equations. We have
v = A(i,j)*X - Y
B(i,j) = v * v'
= (A(i,j)*X - Y) * (A(i,j)*X - Y)'
= A(i,j)*X * conj(A(i,j))*X' - Y * conj(A(i,j))*X'
- A(i,j)*X * Y' + Y * Y'
= A(i,j)*conj(A(i,j)) * X*X' - conj(A(i,j)) * Y*X' - A(i,j) * X*Y' + Y*Y'
So a first result would be
B = A.*conj(A) * (X*X') - conj(A) * (Y*X') - A * (X*Y') + Y*Y'
In the case of real matrices/vectors, one has the identities
X*Y' == Y*X'
A == conj(A)
which means, you can reduce the expression to
B = A.*A * (X*X') - 2*A * (X*Y') + Y*Y'
= A.^2 * (X*X') - 2*A * (X*Y') + Y*Y'
An alternative method:
X = [1 2 3]
Y = [4 5 6]
A = [1 2 3; 4 5 6; 7 8 9]
V = bsxfun(#minus, A(:)*X, [4 5 6])
b = sum((V.^2)')
B = reshape(b , 3, 3)
I get the result:
B = 27 5 11
45 107 197
315 461 635