Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.

조회 수: 167 (최근 30일)
I have an "unable to perform assignment because the indices on the left side are not compatible with the size of the right side" error when i try to run this code. Could you help how can i solve this problem?
Error in (line 15)
z2(i,j)=f0+A0'*X;
clc;
clear;
a=[0.3; 0.7];
x0=[1; 1];
f0= 2*(a'*x0)+log(a'*x0);
A0= 2+(1/(a'*x0)) ;
B0= -(1/(a'*x0).^2);
x = 0.1: 0.05: 0.5;
[x1,x2] = meshgrid(x,x);
z1 = 2*(a(1,1)*x1+a(2,1)*x2)+log(a(1,1)*x1+a(2,1)*x2);
n=length(x1);
for i=1:n
for j=1:n
X=[x1(i,j)-x0(1,1); x2(i,j)-x0(2,1)];
z2(i,j)=f0+A0'*X;
z3(i,j)=f0+A0'*X+0.5*X'*B0*X;
end
end
colormap(gray);
mesh(x1,x2,z1)
hold on
mesh(x1,x2,z2)
mesh(x1,x2,z3)
xlabel('x1');
ylabel('x2');
hold off

채택된 답변

KSSV
KSSV 2021년 10월 8일
Your LHS is a single elememnt and RHS is a 1x2 matrix, which cannot be saved.
May be you want this?
a=[0.3; 0.7];
x0=[1; 1];
f0= 2*(a'*x0)+log(a'*x0);
A0= 2+(1/(a'*x0)) ;
B0= -(1/(a'*x0).^2);
x = 0.1: 0.05: 0.5;
[x1,x2] = meshgrid(x,x);
z1 = 2*(a(1,1)*x1+a(2,1)*x2)+log(a(1,1)*x1+a(2,1)*x2);
n=length(x1);
z2 = zeros(size(x1)) ;
z3 = zeros(size(x2)) ;
for i=1:n
for j=1:n
X=[x1(i,j)-x0(1,1); x2(i,j)-x0(2,1)];
z2(i,j)=f0+A0'*X(1);
z3(i,j)=f0+A0'*X(2)+0.5*X'*B0*X;
end
end
colormap(gray);
mesh(x1,x2,z1)
hold on
mesh(x1,x2,z2)
mesh(x1,x2,z3)
xlabel('x1');
ylabel('x2');
hold off

추가 답변 (1개)

Anis Syafiqah
Anis Syafiqah 2023년 2월 1일
편집: Anis Syafiqah 2023년 2월 1일
I also encounter the same "Unable to perform assignment because the indices on the left side are not compatible with the size of the right side" error. I tried preallocating it by using the zeros for numeric arrays but I still cannot solve it.
The error is at the %Exact Solution part (line 44)
FF(j)=fex(x);
Could somebody help me solve this problem?
% Runge Kutta of order 5
clc
clear
% y''=-4y+2, y(0)=1, y'(0)=1
fy=@(x,y,z) -2;
fz=@(x,y,z) 2-4*y;
fex =@(x) ((1/2*sin(2*x))+(1/2*cos(2*x))+1/2);
x(1)=0;
z(1)=1;
y(1)=1;
h=0.1;
xfinal=1;
N=ceil((xfinal-x(1))/h);
for j=1:N
x(j+1)=x(j)+h;
k1y=fy(x(j),y(j),z(j));
k1z=fz(x(j),y(j),z(j));
k2y=fy(x(j),(y(j)+(1/3*h*k1y)),(z(j)+(1/3*h*k1y)));
k2z=fz(x(j),(y(j)+(1/3*h*k1z)),(z(j)+(1/3*h*k1z)));
k3y=fy(x(j),(y(j)+(2/3*h*k2y)),(z(j)+(2/3*h*k2y)));
k3z=fz(x(j),(y(j)+(2/3*h*k2z)),(z(j)+(2/3*h*k2z)));
k4y=fy(x(j),(y(j)+(h*((-167765027/45900120)*k1y + (43549/7217)*k2y-(30361/14840)*k3y))),(z(j)+(h*((-167765027/45900120)*k1y + (43549/7217)*k2y-(30361/14840)*k3y))));
k4z=fz(x(j),(y(j)+(h*((-167765027/45900120)*k1z + (43549/7217)*k2z-(30361/14840)*k3z))),(z(j)+(h*((-167765027/45900120)*k1z + (43549/7217)*k2z-(30361/14840)*k3z))));
k5y=fy(x(j),(y(j)+(h*((-51638854921283/28366716018615)*k1y + (35525/9169)*k2y - (27646/19955)*k3y- (10643/155037)*k4y))),(z(j)+(h*((-51638854921283/28366716018615)*k1y + (35525/9169)*k2y - (27646/19955)*k3y- (10643/155037)*k4y))));
k5z=fz(x(j),(y(j)+(h*((-51638854921283/28366716018615)*k1z + (35525/9169)*k2z - (27646/19955)*k3z- (10643/155037)*k4z))),(z(j)+(h*((-51638854921283/28366716018615)*k1z + (35525/9169)*k2z - (27646/19955)*k3z- (10643/155037)*k4z))));
k6y=fy(x(j),(y(j)+(h*((-9030268043/1401332565)*k1y + (736810/53619)*k2y - (28702/5227)*k3y- (7/5)*k4y +(3/5)*k5y))),(z(j)+(h*((-9030268043/1401332565)*k1y + (736810/53619)*k2y - (28702/5227)*k3y- (7/5)*k4y +(3/5)*k5y))));
k6z=fz(x(j),(y(j)+(h*((-9030268043/1401332565)*k1z + (736810/53619)*k2z - (28702/5227)*k3z- (7/5)*k4z +(3/5)*k5z))),(z(j)+(h*((-9030268043/1401332565)*k1z + (736810/53619)*k2z - (28702/5227)*k3z- (7/5)*k4z +(3/5)*k5z))));
y=zeros(size(k6y));
z=zeros(size(k6z));
y(j+1)=y(j)+(h/144)*((14*k1y)+(48*k2y)+(162*k3y)+(33*k4y)-(125*k5y)+(12*k6y));
z(j+1)=z(j)+(h/144)*((14*k1z)+(48*k2z)+(162*k3z)+(33*k4z)-(125*k5z)+(12*k6z));
%Exact solution
FF(j)=fex(x);
xx(j)=x;
%Error
Err(j) = abs(FF(j)-y(j));
fprintf('%.1f\t\t%7.8f\t\t%7.8f\t\t%7.8f\n', xx(j), FF(j), y(j), Err(j))
end

카테고리

Help CenterFile Exchange에서 Programming에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by