Solving system of three equations with three unknown variables - matlab

I am trying to solve a system of three equations with three unknown variables.
A1=(x+y)/2+(x-y)/2*cos(2*phi)+z*sin(2*phi)/2
A2=(x+y)/2-(x-y)/2*cos(2*phi)-z*sin(2*phi)/2
A3=-(x-y)/2*sin(2*phi)+z*cos(2*phi)
where A1, A2, A3, and phi are known and x,y, and z are unknown.
I used below code but it does not work. I got the solution as symbols.
clear;
clc;
A1=50;
A2=37.5;
A3=125.6;
phi=28;
syms x y z
eqn1 = (x+y)/2+(((x-y)/2)*cosd(2*phi))+(z*sind(2*phi))/2== A1;
eqn2 = (x+y)/2+(((x-y)/2)*cosd(2*phi))-(z*sind(2*phi))/2== A2;
eqn3 = (((x-y))*sind(2*phi))+(z*cosd(2*phi))== A3;
[A,B] = equationsToMatrix([eqn1, eqn2, eqn3], [x, y, z]);
X_1 = linsolve(A,B);
Thanks

You can skip using equationsToMatrix and linsolve and just use solve. You are already using the symbolics toolbox, so why would you want to convert the system into a matrix of coefficients and solve it that way? Just use the actual equations directly.
A1=50;
A2=37.5;
A3=125.6;
phi=28;
syms x y z
eqn1 = (x+y)/2+(((x-y)/2)*cosd(2*phi))+(z*sind(2*phi))/2== A1;
eqn2 = (x+y)/2+(((x-y)/2)*cosd(2*phi))-(z*sind(2*phi))/2== A2;
eqn3 = (((x-y))*sind(2*phi))+(z*cosd(2*phi))== A3;
[X, Y, Z] = solve(eqn1, eqn2, eqn3);
I get:
X = (sym)
69370560820559
──────────────
926177760500
Y = (sym)
-61526962823521
────────────────
926177760500
Z = (sym)
2910
────
193
Note that I'm using Octave instead of MATLAB (on my current system, I don't have access to the symbolic toolbox) so the output may be a bit different in format. You probably also want this in real (floating-point) form, so an additional cast to double for the outputs should help:
X = double(X);
Y = double(Y);
Z = double(Z);
By doing this, we get:
>> format long g;
>> X
X = 74.8998343288972
>> Y
Y = -66.4310518429048
>> Z
Z = 15.0777202072539

Related

Matlab solving ODEs can not understand this function description

I have stumbled upon this matlab code that solves this ODE
y'''(t) + a y(t) = -b y''(t) + u(t)
but I am confused by the ode_system function definition, specifically by the y(2) y(3) part. I would greatly appreciate if someone can shed some light
y(2) y(3) part in the ode_system function confuses me and how it contributes to overaal solution
% Define the parameters a and b
a = 1;
b = 2;
% Define the time horizon [0,1]
time_horizon = [0, 1];
% Define the initial conditions for y, y', and y''
initials = [0; 0; 0];
% Define the function handle for the input function u(t)
%sin(t) is a common example of a time-varying function.
% You can change the definition of u to any other function of time,
% such as a constant, a step function, or a more complex function, depending on your needs
u = #(t) sin(t);
% Define the function handle for the system of ODEs
odefunction = #(t, y) ode_system(t, y, a, b, u);
% Solve the ODEs using ode45
[t, y] = ode45(odefunction, time_horizon, initials);
% Plot the solution
plot(t, y(:,1), '-', 'LineWidth', 2);
xlabel('t');
ylabel('y');
function dydt = ode_system(t, y, a, b, u)
%Define the system of ODEs
dydt = [y(2); y(3); -b*y(3) + u(t) - a*y(1)];
end
This is more of a maths question than a Matlab one.
We would like to rewrite our ODE equation so that there is a single time derivative on the left-hand side and no derivatives on the right.
Currently we have:
y'''(t)+ay(t)=-by''(t)+u(t)
By letting z = y' and x = z' (= y''), we can rewrite this as:
x'(t)+a y(t)=-b x(t)+u(t)
So now we have 3 equations in the form:
y' = z
z' = x
x' = -b * x + u - a *y
We can also think of this as a vector equation where v = (y, z, x).
The right-hand side would then be,
v(1)' = v(2)
v(2)' = v(3)
v(3)' = -b * v(3) + u - a * v(1)
which is what you have in the question.

Matrix equation solving in Matlab

Motivation:
I have to iteratively determine transfer functions of a system depending on its parameters. From the block diagram, I know the matrix equations and now have to combine and solve them to obtain the transfer function. The system is not given in any common form, so I can't just apply standard functions as for example ss2f.
Problem:
To test my equation solving script, I started with a problem I know the solution to:
The transfer function G of this system can be directly calculated as follows with s as the Laplace variable:
Now I want to obtain the same solution with the following script that I wrote. Don't worry about the weird numbers of A and B, the system was determined via model linearization. It definitely has a valid transfer function though, as above formula has a valid result. However, the last line of the script gives me:
ans =
struct with fields:
g1: [0×1 sym]
g3: [0×1 sym]
g2: [0×1 sym]
g4: [0×1 sym]
Below is my code. Hope you can help me with that. Thanks in advance!
System matrix: 4x4
A = [-0.868119260443192,38.6220008060345,-3.41464245222828,0;-0.977364157120952,-0.710033524230589,0.0269276394855778,0.576935314629657;4.02838694184468,-15.0624095995407,-31.0586647741313,0;0.0225429037937697,0,1,-3.32771392487994e-30];
% Input Matrix 4x2
B = [-7.66837152645279,-21.1508491865230;-0.0466721502330690,0.198650074689473;-112.758863363750,-2.59747270711686;0,0];
% Output matrix 2x4
C = [0 0 1 0; 0 1 0 0];
% Transfer function
syms g1 g2 g3 g4;
G = [g1 g2; g3 g4];
% System vector and derivative
syms x1 x2 x3 x4 xp1 xp2 xp3 xp4;
x = [x1;x2;x3;x4];
xp = [xp1;xp2;xp3;xp4];
% Input u and output y
syms u1 u2;
u = [u1;u2];
syms y1 y2;
y = [y1;y2];
% System equations
eqn1 = xp == A * x + B * u;
eqn2 = y == C * x;
% Transfer function equation
eqn3 = y == G * u;
% xp is derivative of x
syms s;
eqn4 = s .* x == xp;
solve(eqn1, eqn2, eqn3, eqn4, G)

How to numerically solve a system with two matrices in Matlab?

I'm trying to numerically find the solution to A*cos x +B*sin x = C where A and B are two known square matrices of the same size (for example 100x100), and C is a known vector (100x1).
Without the second term (i.e. with a single matrix), I will use Jacobi or Gauss-Seidel to solve this problem and get x but here, I don't see how to proceed to solve this problem in Matlab.
May be, it would be useful to solve the problem as : A*X + B*sqrt(1-X^2) = C.
I would greatly appreciate any help, ideas or advices
Thanks in advance
If I understood you correctly, you could use fsolve like this (c and X are vectors):
A = ones(2,2);
B = ones(2,2);
c = ones(2,1);
% initial point
x0 = ones(length(A), 1);
% call to fsolve
sol = fsolve(#(x) A * cos(x) + B*sin(x) - c, x0);
Here, we solve the nonlinear equation system F(x) = 0 with F: R^N -> R^N and F(x) = A * cos(x) + B*sin(x) - c.
Only for the sake of completeness, here's my previous answer, i.e. how one could do it in case C and X are matrices instead of vectors:
A = ones(2,2);
B = ones(2,2);
C = ones(2,2);
% initial point
x0 = ones(numel(A), 1);
% call to fsolve
fsolve(#(x) fun(x, A, B, C), x0)
function [y] = fun(x, A, B, C)
% Transform the input vector x into a matrix
X = reshape(x, size(A));
% Evaluate the matrix equation
Y = A * cos(X) + B*sin(X) - C;
% flatten the matrix Y to a row vector y
y = reshape(Y, [], 1);
end
Here, the idea is to transform the matrix equation system F: R^(N x N) -> R^(N x N) into a equivalent nonlinear system F: R^(N*N) -> R^(N*N).

solving differential equation using matlab

I have a question and just need you to tell me the steps I have to do.
I have an equation with boundary conditions.the question is how can I find f(x)?
I don't want to use the predefined Matlab.Please just show me the steps I need to solve this problem.
Thanks...
Simply you can use symbolic math toolbox in MATLAB:
syms f(x) % Define symbolic function
F = dsolve(diff(f,2) + diff(f,1) + 200*f == 0);
% Find C1 and C2 constants
syms C1 C2 L
BC_eq(1) = subs(F, x, 0) - 0;
BC_eq(2) = subs(F, x, L) - 100;
C_val = solve(BC_eq, [C1, C2]);
% Substitude C' values in F
F_final = subs(F, {C1, C2}, {C_val.C1, C_val.C2})

Matlab gradient and hessian computation for symbolic vector function

I am trying to use the Matlab "gradient" and "hessian" functions to calculate the derivative of a symbolic vector function with respect to a vector. Below is an example using the sigmoid function 1/(1+e^(-a)) where a is a feature vector multiplied by weights. The versions below all return an error. I am new to Matlab and would very much appreciate any advice. The solution may well be under my nose in the documentation, but I have not been able to solve the issue. Thank you in advance for your help!
%version 1
syms y w x
x = sym('x', [1 3]);
w = sym('w', [1 3]);
f = (y-1)*w.*x + log(1/(1+exp(-w.*x)));
gradient(f, w)
%version 2
syms y w1 w2 w3 x1 x2 x3 x w
x = [x1,x2,x3];
w = [w1,w2,w3];
f = (y-1)*w.*x + log(1/(1+exp(-w.*x)));
%version 3
syms y w1 w2 w3 x1 x2 x3
f = (y-1)*[w1,w2,w3].*[x1,x2,x3] + log(1/(1+exp(-[w1,w2,w3].*[x1,x2,x3])));
Thanks, Daniel - it turns out the problem was in not having used dot() to take the dot product of w and x. Both diff() and gradient() gave the same solution, shown below:
syms y
x = sym('x', [1 3]);
w = sym('w', [1 3]);
f = (y-1)*dot(w,x) + log(1/(1+exp(dot(-w,x))));
diff(f, w(1))
gradient(f, w(1))
%ans =
%x1*(y - 1) + (x1*exp(- x1*conj(w1) - x2*conj(w2) - x3*conj(w3)))/
(exp(-x1*conj(w1) - x2*conj(w2) - x3*conj(w3)) + 1)