MATLAB Answers

Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-2.

5 views (last 30 days)
Shaan Singh
Shaan Singh on 26 Mar 2021
Commented: darova on 1 Apr 2021
I get the error "Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-2." when I run the script. I am not sure where I am going wrong.
function script:
function [w1i, w2i, ti] = Basic_Euler(h ,m1 ,m2 , k1, k2, k3, c1, c2, c3, z1, z2,N,t0, x1,x2, tf)
neqn1 = length (x1);
neqn2 = length (x2);
ti = linspace ( t0, tf, N+1 );
w1i = [zeros( neqn1, N+1 ) ];
w2i = [zeros( neqn2, N+1 ) ];
for i = 1:N
z1(i+1) = z1(i) + h.*(((c2.*z2(i))-((c1+c2).*z1(i))+(k2.*x2(i))-((x1(i).*(k1+k2))))./m1);
x1(i+1) = x1(i) + h.*(z1(i));
z2(i+1) = z2(i) + h.*(((c2.*z1(i))-((c2+c3).*z2(i))+(k2.*x1(i))-((k2+k3).*x2(i)))./m2);
x2(i+1) = x2(i) +h.*(z2(i));
w1i(1:neqn1,i+1) = x1;
w2i(1:neqn2,i+1) = x2;
end
end
Master Script:
function Ass2()
close all
t0 = 0;
tf = 100;
h = 0.1;
N = (tf - t0)./h;
k1 = 3;
k2 = 2;
k3 = 1;
c3 = 0.01;
m2 = 4;
c1 = 0.03;
c2 = 0.02;
m1 = 1;
x1 = -1;
x2 = 6;
z1 = -1;
z2 = 3;
[x1,x2, ti] = Basic_Euler(h ,m1 ,m2 , k1, k2, k3, c1, c2, c3, z1, z2,N,t0, x1,x2, tf);
figure1 = figure;
axes1 = axes('Parent',figure1,'FontSize',20,'FontName','Times New Roman');
box(axes1,'on');
grid(axes1,'on');
hold(axes1,'all');
xlabel('t','FontSize',20,'FontName','Times New Roman');
ylabel('x','FontSize',20,'FontName','Times New Roman');
plot1 = plot(ti,x1,'Parent',axes1,'LineWidth',3);
set(plot1(1),'Marker','diamond','DisplayName','Basic Euler 1');
legend(axes1,'show');
figure2 = figure;
axes1 = axes('Parent',figure2,'FontSize',20,'FontName','Times New Roman');
box(axes1,'on');
grid(axes1,'on');
hold(axes1,'all');
xlabel('t','FontSize',20,'FontName','Times New Roman');
ylabel('x','FontSize',20,'FontName','Times New Roman');
plot1 = plot(ti,x2,'Parent',axes1,'LineWidth',3);
set(plot1(1),'Marker','diamond','DisplayName','Basic Euler 1');
legend(axes1,'show')

Answers (1)

Pavan Guntha
Pavan Guntha on 29 Mar 2021
Hi Shaan,
The error is due to the line 15 of the function Basic_Euler:
w1i(1:neqn1,i+1) = x1;
The size of x1 is getting changed within the function due to the line:
x1(i+1) = x1(i) + h.*(z1(i));
Because of this reason, the size mismatch is happening. The possible solution might be to change the line 15 & 16 in the function as follows:
w1i(1:neqn1,i+1) = x1(i+1);
w2i(1:neqn2,i+1) = x2(i+1);
Hope this helps!

Community Treasure Hunt

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

Start Hunting!