solving Linear differential equation
6 次查看(过去 30 天)
显示 更早的评论
A = [[(sin(10*(pi/180))) * (sin(90*(pi/180))),(sin(10*(pi/180)))*(cos(90*(pi/180))), cos(10*(pi/180))],[(sin(10*(pi/180))) * (sin(180*(pi/180))),(sin(10*(pi/180)))*(cos(180*(pi/180))), cos(10*(pi/180))],[(sin(0*(math.pi/180))) * (sin(0*(pi/180))),(sin(0*(pi/180)))*(cos(0*(pi/180))), cos(0*(pi/180))]]
This is my code in python . Can you please translate this code to matlab for solving this equation using linear differential eguation .It is in form of a 3x3 matrix.
0 个评论
回答(1 个)
Luna
2020-3-10
编辑:Luna
2020-3-10
Hi there!
There are 2 ways of doing this. It depends on your B vector according to Ax = B equation system. Your solutions below. You should read the links I mentioned.
(Note: Matlab understands pi as pi. 3,14... no need extra library for that.)
If your case is Linear Equations:
Read the link: Solve System of Linear Equations
% definition of A:
A = [...
[(sin(10*(pi/180))) * (sin(90*(pi/180))),(sin(10*(pi/180)))*(cos(90*(pi/180))), cos(10*(pi/180))];...
[(sin(10*(pi/180))) * (sin(180*(pi/180))),(sin(10*(pi/180)))*(cos(180*(pi/180))), cos(10*(pi/180))];...
[(sin(0*(pi/180))) * (sin(0*(pi/180))),(sin(0*(pi/180)))*(cos(0*(pi/180))), cos(0*(pi/180))]...
];
% Solution 1:
% AX = B. Depends on what B is.
B = [1,2,3]'; % I prefer 3x1 vertical vector. You should define your own B.
X = linsolve(A,B);
% Solution 2:
% Define your equations with symbolic characters.
syms x y z;
eqn1 = ((sin(10*(pi/180))) * (sin(90*(pi/180))))* x + ((sin(10*(pi/180)))*(cos(90*(pi/180))))*y + (cos(10*(pi/180)))*z == 1; % this equation equals first element of B.
eqn2 = ((sin(10*(pi/180))) * (sin(180*(pi/180))))*x + (sin(10*(pi/180)))*(cos(180*(pi/180)))*y + cos(10*(pi/180))*z == 2;
eqn3 = ((sin(0*(pi/180))) * (sin(0*(pi/180))))*x + ((sin(0*(pi/180)))*(cos(0*(pi/180))))*y +(cos(0*(pi/180)))*z == 3;
% Create your A matrix and B vector:
[A,B] = equationsToMatrix([eqn1, eqn2, eqn3], [x, y, z]);
X = linsolve(A,B); % this result is symbolic.
% convert to numeric:
Result = double(X);
disp(Result);
If your case is Differential Equations
Read this link: Solve a System of Differential Equations
%% For System of Differential Equations:
% definition of A:
A = [...
[(sin(10*(pi/180))) * (sin(90*(pi/180))),(sin(10*(pi/180)))*(cos(90*(pi/180))), cos(10*(pi/180))];...
[(sin(10*(pi/180))) * (sin(180*(pi/180))),(sin(10*(pi/180)))*(cos(180*(pi/180))), cos(10*(pi/180))];...
[(sin(0*(pi/180))) * (sin(0*(pi/180))),(sin(0*(pi/180)))*(cos(0*(pi/180))), cos(0*(pi/180))]...
];
syms x(t) y(t) z(t)
ode1 = diff(x) == ((sin(10*(pi/180))) * (sin(90*(pi/180))))* x + ((sin(10*(pi/180)))*(cos(90*(pi/180))))*y + (cos(10*(pi/180)))*z;
ode2 = diff(y) == ((sin(10*(pi/180))) * (sin(180*(pi/180))))*x + (sin(10*(pi/180)))*(cos(180*(pi/180)))*y + cos(10*(pi/180))*z;
ode3 = diff(z) == ((sin(0*(pi/180))) * (sin(0*(pi/180))))*x + ((sin(0*(pi/180)))*(cos(0*(pi/180))))*y +(cos(0*(pi/180)))*z;
odes = [ode1;ode2;ode3];
S = dsolve(odes)
You will get a struct S like below:
S =
struct with fields:
y: [1×1 sym]
x: [1×1 sym]
z: [1×1 sym]
To reach each of them use dot notation:
xSol(t) = S.x;
ySol(t) = S.y; % etc..
2 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Equation Solving 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!