MATLAB Answers

error in matrix multiplication while solving system of differential equations

14 views (last 30 days)
Meenakshi Tripathi
Meenakshi Tripathi on 6 Apr 2021
Commented: Meenakshi Tripathi on 7 Apr 2021 at 16:31
function comb_thesis
clc
clear all
E = [0.5 -2.5 0 0 0 0 0 0 0 0 0 0;3 -3 4 0 0 0 0 0 0 0 0 0;2 -1 3 0 0 0 0 0 0 0 0 0;zeros(3) eye(3) zeros(3) zeros(3);zeros(3) zeros(3) zeros(3) zeros(3); 0 0 0 0 0 0 0 0 0 0 0 0]
A = [-1 4.5 -0.5 0 0 0 0 0 0 0 0 0;-7 7 -8 0 0 0 0 0 0 0 0 0;-5 3 -6 0 0 0 0 0 0 0 0 0;0 0 0 -0.75 -1 0.25 0 0 0 51.3257 11.2723 0;0 0 0 0 -2 0 0 0 0 41.5581 7.8378 0;0 0 0 0.25 1 -0.75 0 0 0 -24.3673 -6.2663 0;0 0 0 1 0 0 -1 0 0 -0.4488 2.4167 0;0 0 0 0 1 0 0 -1 0 -0.0898 0.4833 0;0 0 0 0 0 1 0 0 -1 0.2693 -1.4500 0;1 0 1 0 0 0 0 0 0 -1 0 0]
B = [1 0 1;0 1 1;1 0 -1;0.0517 -0.2759 0.7068;-0.2241 -0.1379 -0.3965;-0.0517 0.2759 -0.7068;zeros(3);0 0 0]
tspan = 0:0.1:10;
x0 = [1 0 -1 10 11 6 0 0 0 0];
opt = odeset('RelTol', 1e-6,'Mass',E);
[~,x] = ode15s(@odecomb,tspan,x0,opt);
function dxdt = odecomb(t,x)
dxdt = A*x + B*[e^(-t)*sint;0.2*sin(2*t);0.2*sin(3*t)]
end
end
Error that i am facing is -
Error using *
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix
matches the number of rows in the second matrix. To perform elementwise multiplication, use '.*'.
Error in comb_thesis/odecomb (line 31)
dxdt = A*x + B*[e^(-t)*sint;0.2*sin(2*t);0.2*sin(3*t)]
i have checked several times for dimensions and matrix multiplication and still i am not able to resolving it. Kindly resolve it.

Answers (1)

John D'Errico
John D'Errico on 6 Apr 2021
Edited: John D'Errico on 6 Apr 2021
What you do not understand is that the exponential function is exp. Thus, instead of writing e^(-t), you write exp(-t).
Next, you should probably use .* as the operator to multiply terms, though I'm pretty sure ODE15s will not call the code with multiple time points, so this is actually not a problem if t is a scalar.
And finally, MATLAB is not smart enough to know that sint is actually intended as sin(t).
dxdt = A*x + B*[exp(-t).*sin(t);0.2*sin(2*t);0.2*sin(3*t)]
Those were the obvious errors I see. What else?
You pass in Xo as a ROW vector. But then you try to MULTIPLY A*x inside your code.
x0 = [1 0 -1 10 11 6 0 0 0 0];
size(x0)
ans =
1 10
A is a 10x12 array.
size(A)
ans =
10 12
You CANNOT multiply a 10x12 array with a 1x10 matrix as you wrote it. A*x does not conform for matrix multiplication. If you did think to use x*A, which would conform, that result would be a 1x12 vector, which would still be a problem when you add it to a 10x1 vector, the result of B*[] as you write it.
So you have a serious error aiting to happen down the line still.
I'd suggest you start by taking the MATLAB Onramp tutorials, since there are basic things you have not learned in MATLAB. At the very least, you need to learn to check your code far more carefully. Read your code. Are there obvious errors? LOOK AT IT, BEFORE YOU CLICK RUN. That will save you much time asking obvious questions when you could just have fixed the obvious errors before even trying to run the code.
It is also a very good idea to test your kernel function odecomb at the command line to make sure it runs. Pass in a vector x and a time t. Does it return what it should? Does it return what ode15s will expect?
  1 Comment
Meenakshi Tripathi
Meenakshi Tripathi on 7 Apr 2021 at 16:31
Thank you for the suggestions.
I might have not good knowledge of matlab but i have knowledge of mathematics. I checked for dimensions but perhaps didn't find mistakes. Next time i'll be careful.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by