solving Linear differential equation

4 views (last 30 days)
nayana k
nayana k on 10 Mar 2020
Commented: Luna on 10 Mar 2020
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.

Answers (1)

Luna
Luna on 10 Mar 2020
Edited: Luna on 10 Mar 2020
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 Comments
nayana k
nayana k on 10 Mar 2020
Thanks for your response and you helps me alot.
Luna
Luna on 10 Mar 2020
Your welcome. Please accept answer if it is a solution :)

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!