Error in Unable to perform assignment because the left and right sides have a different number of elements.
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I'm having an issue with trying to work out a error for a finte difference equation but keep getting the error of 'Unable to perform assignment because the left and right sides have a different number of elements'.
A= (32*pi)/7 ;
f = @(x) 0.*( x<25/64 & x>39/64) +cos(A*(x-0.5)).*( (25/64)<=x & x<=39/64);
g = @(x) 0.*( x<25/64 & x>=39/64) +-A*sin(A*(x-0.5)).*( (25/64)<x & x<=39/64);
p=-12:1:-5;
h=2.^p;
x = 0:h:1;
n = length(x);
for m=1:1
y=(f(x)).^m;
z=g(x).*m.*(f(x).^(m-1));
dy = zeros(n,1);
for i=3:n-2
dy(i) = (y(i-2)-8*y(i-1)+8*y(i+1)-y(i+2))./(12*h) ;
end
e= (abs(z-dy')); % The error function
% Now when you plot the derivatives, skip the first and the last point
figure;
plot(x,y,'r');
hold on;
pause
plot(x(2:end-1), dy(2:end-1),'b');
grid on;
pause
plot(x,z,'g');
plot(x,e,'k');
legend('cos(x)', 'dy', '-sinx','Error')
hold off
end
0 comentarios
Respuestas (1)
Image Analyst
el 9 de Jul. de 2022
Did you try normal debugging techniques?
like this:
tempVar = (y(i-2)-8*y(i-1)+8*y(i+1)-y(i+2))./(12*h);
whos h
whos tempVar
dy(i) = (y(i-2)-8*y(i-1)+8*y(i+1)-y(i+2))./(12*h);
Note that
Name Size Bytes Class Attributes
h 1x8 64 double
Name Size Bytes Class Attributes
tempVar 1x8 64 double
So, since h is a vector, tempVar is also a vector of 8 elements. You cannot put those 8 elements into the single i'th element of dy. You can put only one element in. Maybe you want to just pick out one of the 8 values of h to put into the equation?
0 comentarios
Ver también
Categorías
Más información sobre Annotations en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!