# solving Linear differential equation

조회 수: 4(최근 30일)
nayana k 2020년 3월 10일
댓글: Luna 2020년 3월 10일
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.

댓글을 달려면 로그인하십시오.

### 답변(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:
% 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
%% 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표시 이전 댓글 수: 1숨기기 이전 댓글 수: 1
Luna 2020년 3월 10일
Your welcome. Please accept answer if it is a solution :)

댓글을 달려면 로그인하십시오.

### 범주

Find more on Ordinary Differential Equations in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by