Ploting solutions to linear equations

2 visualizzazioni (ultimi 30 giorni)
Consider the linear system of equations x' = Ax. Let x(t)=[x1(t), x2(t), x3(t)]T be the unique solution such that x(0) = v1. In the same figure, I want to plot plot each of the functions x1(t), x2(t) and x3(t). I dont think I should have to use ODE 45. I have already looked on the matlab website to no avail.
M = [7 7 7; 2 6 7; 4 3 1];
B = transpose(M);
A = M+B;
[V,D]=eig(A);
lambda1= -5.9049;
v1= [-0.3429 -0.3208 0.8829];
% part c
syms x(t) y(t) % I use x(t) and y(t) to represent x1' and x2'
Y = [x; y];
odes = diff(Y) == A*Y %matlab keeps saying there is a problem with this line and I dont know why
[xSol(t), ySol(t)] = dsolve(odes);
xSol(t) = simplify(xSol(t));
ySol(t) = simplify(ySol(t));
C = Y(0) == [-0.3429 -0.3208 0.8829];
[xSol(t), ySol(t)] = dsolve(odes,C)
clf
fplot(ySol)
hold on
fplot(xSol)
grid on
savefig('symmetric.fig')

Risposta accettata

Star Strider
Star Strider il 16 Nov 2019
The ‘A*Y’ operation attempts to multiply (3x3) matrix ‘A’ by a (2x1) vector ‘Y’. That will just never work!
The ‘Y’ vector must be (3x1) to do that multiplication.
  2 Commenti
Mike Chan
Mike Chan il 19 Nov 2019
Hello,
I made the code in terms of 3 variables now . The code complies but, matlab is returning [ empty sym ] and a blank graph. What does that mean?
Thanks
M = [7 7 7; 2 6 7; 4 3 1];
B = transpose(M);
A = M+B;
[V,D]=eig(A);
lambda1= -5.9049;
v1= [-0.3429 -0.3208 0.8829];
% part c
syms x(t) y(t) z(t)
Y = [x; y; z];
odes = diff(Y) == A*Y
[xSol(t), ySol(t),zSol(t)] = dsolve(odes);
xSol(t) = simplify(xSol(t));
ySol(t) = simplify(ySol(t));
zSol(t) = simplify(zSol(t));
C = Y(0) == [-0.3429 -0.3208 0.8829];
[xSol(t), ySol(t),zSol(t)] = dsolve(odes,C)
clf
fplot(ySol)
hold on
fplot(xSol)
grid on
fplot(zSol)
savefig('symmetric.fig')
Star Strider
Star Strider il 19 Nov 2019
The Symbolic Math Toolbox appears not to be able to solve that, even though it is a linear system, and actually has an analytic solution.
Try this instead:
M = [7 7 7; 2 6 7; 4 3 1];
B = transpose(M);
A = M+B;
[V,D]=eig(A);
lambda1= -5.9049;
v1= [-0.3429 -0.3208 0.8829];
% part c
syms x(t) y(t) z(t) t Y
Y = [x; y; z];
odes = diff(Y) == A*Y
[VF,Sbs] = odeToVectorField(odes)
odesfcn = matlabFunction(VF, 'Vars',{'t','Y'})
t = linspace(0, 0.3, 50);
Y0 = [-0.3429 -0.3208 0.8829];
[T,xyz] = ode45(odesfcn, t, Y0);
figure
plot(t,xyz)
grid
legend(string(Sbs))
Parenthetically, I have no idea what the problem is with odeFunction. It used to take a cell array of symbolic variables:
odesfcn = matlabFunction(VF, 'Vars',{t,Y})
without problems, and that is still in the documentation for it. Now, it requires them to be a cell array of character vectors, and will not work without defining them with single quotes, as I did here.

Accedi per commentare.

Più risposte (0)

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by