In an assignment A(:) = B, the number of elements in A and B must be the same.

1 Ansicht (letzte 30 Tage)
Hi all
I'm working on a code for my numerical differential equations class and Ive run into some trouble.. I'm fairly new with matlab. I'm creating a code to perform the euler method on a system of four second order differential equations. So, I created an m. file with my function which puts the 8 first order equations into a matrix. I then use this file to perform the euler method on the matrix, however when I run the program it gives me back the error "In an assignment A(:) = B, the number of elements in A and B must be the same.". as far as I can tell both the input matrix from the function and the output matrix from the for loop should have the same number of rows so it should work fine, no? any help would be great, Thanks!
%function code
function dx = projectmatrix2(t,u1,u2,u3,theta)
k1 = 200000;
k2 = 95000;
k3 = 35000;
m1 = 2.5;
m2 = 2.5;
m3 = 325;
c = 1500;
r = 1;
I = 93;
dx = zeros(8,1);
dx(1) = u1(2);
dx(2) = ((k1 - k2)/m1)*u1 + (k3/m1)*u3 + (c/m1)*u1(2) -(c/m1)*u3(2);
dx(3) = u2(2);
dx(4) = ((k1-k3)/m2)*u2 +(k3/m2)*u3 + (c/m2)*u2(2) - (c/m2)*u3(2);
dx(5) = u3(2);
dx(6) = (k2/m3) *u1 + (k3/m3)*u2 + ((-k2-k3)/m3) *u3 -(c/m3)*u1(2) - (c/m3)*u2(2) + ((2*c)/m3)*u3(2);
dx(7) = theta(2);
dx(8) = -((r*k3)/I)*u1 + ((r*k3)/I)*u2 - ((r*k3 + r*k2)/I)*u3;
end
%new file with euler method applied
Tsim = 4;
h = .1;
N = Tsim/h;
x = zeros(8,N);
x(1,1) = 2;
x(2,1) = 0;
x(3,1) = 0;
x(4,1) = 0;
x(5,1) = 0;
x(6,1) = 0;
x(7,1) = 0;
x(8,1) = 0;
t = zeros(1,N);
for k=1:N-1
t(k+1) = t(k) + h;
x(:,k+1) = x(:,k) + h * projectmatrix2(t(k),x(1:2,k),x(3:4,k),x(5:6,k),x(7:8,k));
end

Akzeptierte Antwort

Andrew Newell
Andrew Newell am 17 Apr. 2017
Look at the first part of a line of code in projectmatrix.m:
dx(2) = ((k1 - k2)/m1)*u1
On the left side, you have a scalar. On the right, you have a vector u1 of length 2. Another term in this line is (c/m1)*u1(2), so perhaps you mean to write
dx(2) = ((k1 - k2)/m1)*u1(1)
Note that there are several more uses of u1, u2 and u3 that have the same problem.

Weitere Antworten (0)

Kategorien

Mehr zu Linear Algebra finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by