Unable to perform assignment because the left and right sides have a different number of elements.

1 visualizzazione (ultimi 30 giorni)
The code is for basic Euler to solve Second order ODEs, I'm really stuck for how to resolve this error message:
'Unable to perform assignment because the left and right sides have a different number of elements.
Error in basicEuler (line 23)
v1(i + 1) = v1(i) + h*(eq1(x1(i), x2(i), v1(i), v2(i))); '
Any help/advice is welcome
Thank you
function [] = basicEuler(h,N)
h = 0.5; N = 500;
t = 1:h:N;
x1 = zeros(size(t)); x1(1) = -7;
x2 = zeros(size(t)); x2(1) = 7;
x3 = zeros(size(t)); x3(1) = -3;
v1 = zeros(size(t)); v1(1) = -3;
v2 = zeros(size(t)); v2(1) = -2;
v3 = zeros(size(t)); v3(1) = -1;
F = zeros(size(t)); F(1) = myForce(0);
for i = 1:(length(t)-1)
F(i + 1) = myForce(i);
x1(i + 1) = x1(i) + h*v1(i);
x2(i + 1) = x2(i) + h*v2(i);
x3(i + 1) = x3(i) + h*v3(i);
v1(i + 1) = v1(i) + h*(eq1(x1(i), x2(i), v1(i), v2(i)));
v2(i + 1) = v2(i) + h*(eq2(x1(i), x2(i), x3(i), v1(i), v2(i), v3(i)));
v3(i + 1) = v3(i) + h*(eq3(x2(i), x3(i), v2(i), v3(i), F(i)));
end
figure(1);
plot(t,x1)
title('basicEuler x1(t)')
xlabel('Time')
ylabel('Displacement')
figure(2);
plot(t,x2)
title('basicEuler x2(t)')
xlabel('Time')
ylabel('Displacement')
figure(3);
plot(t,x3)
title('basicEuler x3(t)')
xlabel('Time')
ylabel('Displacement')
end
  4 Commenti
Cody
Cody il 16 Gen 2024
As seperate scripts
function [equation1] = eq1(x1,x2,v1,v2)
global c1 c2 c3 k1 k2 k3 m1 m2 m3
equation1 = (-( v1*( c1 + c2 ) + ( v2*c2 ) - ( x1*( k1 + k2 ) ) + ( x2*k2 ) ) ) /m1 ;
end
function [equation2] = eq2(x1,x2,x3,v1,v2,v3)
global c1 c2 c3 k1 k2 k3 m1 m2 m3
equation2 = ( ( v1*c2 ) - ( v2*( c2 + c3 ) ) + ( v3*c3 ) + ( x1*k2 ) - ( x2*( k2 + k3 ) ) + ( x3*k3 ) )/m2 ;
end
function [equation3] = eq3(x2,x3,v2,v3,F)
global c1 c2 c3 k1 k2 k3 m1 m2 m3
equation3 = ( ( v2*c3 ) - ( v3*c3 ) + ( x2*k3 ) - ( x3*k3 ) + F)/m3 ;
end
with c1,c2,c3,k1,k2,k3 being separate variables defined through respective functions

Accedi per commentare.

Risposte (1)

SAI SRUJAN
SAI SRUJAN il 22 Gen 2024
Hi cody,
I understand that you are working solve second-order ODEs using the Euler method. You're encountering an error that says, "Unable to perform assignment because the left and right sides have a different number of elements," which occurs at line 23 in your code.
v1(i + 1) = v1(i) + h*(eq1(x1(i), x2(i), v1(i), v2(i)));
This error typically indicates that when you're trying to assign a value to 'v1(i + 1)', MATLAB expects a single value on the right side of the assignment, but it's receiving an array or a different number of elements instead.
It is critical to verify that the global variables 'c1',' c2', 'c3', 'k1', 'k2', 'k3', 'm1', 'm2', and 'm3', which are utilized within your 'eq1', 'eq2', and 'eq3' functions, are correctly initialized and assigned scalar values prior to invoking the 'basicEuler' function.
Please ensure that the equation, yields a single scalar value as a result.
equation1 = (-( v1*( c1 + c2 ) + ( v2*c2 ) - ( x1*( k1 + k2 ) ) + ( x2*k2 ) ) ) /m1 ;
By ensuring the accuracy of aforementioned details, you should be able to resolve the error.
I hope this helps!

Categorie

Scopri di più su Programming in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by