it says the right side does not equal to the left i dont know what should i do can anyone help me pls?

3 views (last 30 days)
Rula laith
Rula laith on 16 Apr 2021
Commented: Walter Roberson on 17 Apr 2021
clear
close all
clc
L=input('Enter the inductors values in Henry, [L1...Ln]=');
n=length(L);
while(~isreal(L)|| any(L<=0)||(mod(n,2)==0)|| any(n<1))
if(~isreal(L))
L=input('The values of all inductances must be real.\nRe-enter values of the inductors in Henry,[L1...Ln]=');
n=length(L);
elseif(any (L<=0))
L=input('All inductors should be positive (non-zero) real numbers, check the values and enter again.\nRe-enter values of the inductors in Henry,[L1...Ln]=');
n=length(L);
elseif(mod(n,2)==0)
L=input('The number of inductors should be odd.\nRe-enter values of the inductors in Henry,[L1...Ln]=');
n=length(L);
elseif(n<1)
L=input('The minimum value for n is 1.\nRe-enter values of the inductors in Henry,[L1...Ln]=');
n=length(L);
else
break;
end
end
disp('==============================================================================')
R=input('Enter the resistance values in Ohm, [R1...R(n+1)/2]=');
nR=length(R);
n=length(L);
A=((n+1)/2);
while(L)
if(~isreal(R))
R=input('The values of all resistances must be real.\n Re-enter the resistance values in Ohm, [R1...R(n+1)/2]=');
elseif(any (R<=0))
R=input('The values of resistances must be positive (non-zero).\n Re-enter the resistance values in Ohm, [R1...R(n+1)/2]=');
elseif(nR~=A)
B = ['The number of resistor should be ',num2str(A),', i.e. 1/2 number of (number of inductors + 1).'];
disp(B);
R=input('Re-enter the resistance values in Ohm, [R1...R(n+1)/2]=');
nR=length(R);
else
break;
end
end
disp('==============================================================================')
C=input('Enter the capacitance values in Farads, [C1...C(n+1)/2]=');
nC=length(C);
n=length(L);
A=((n+1)/2);
while(L)
if(nC~=A)
B = ['The number of capacitor should be ',num2str(A),', i.e. 1/2 number of (number of inductors + 1).'];
disp(B);
C=input('Enter the capacitance values in Farads, [C1...C(n+1)/2]=');
nC=length(C);
elseif(~isreal(C))
C=input('All capacitances should be positive (non-zero) real numbers, check the values and enter again.\nEnter the capacitance values in Farads, [C1...C(n+1)/2]=');
elseif(any (C<=0))
C=input('The values of capacitors must be positive (non-zero).\nEnter the capacitance values in Farads, [C1...C(n+1)/2]=');
else
break;
end
end
disp('==============================================================================')
Vmag = input('Enter the amplitude of the voltage sources respectively in volt,Vmag=');
while(~isreal(Vmag))
Vmag =input('Voltage magnitudes must be any real number.\nEnter the magnitude of the voltage sources in volt, Vmag=');
end
disp('==============================================================================')
Vphase = input('Enter the phase of the voltage sources respectively in degree,Vphase=');
disp('==============================================================================')
Freq = input('Enter the frequency of the voltage source in rad/s,Freq=');
disp('==============================================================================')
Alpha=input('Enter the Alpha (between 0 and 2) to be used with the Successive Relaxation Method=');
while((Alpha<0)||(Alpha>2)||~isreal(Alpha))
Alpha=input('The Alpha should be positive real number between (0 and 2),check the value and enter it again.\nEnter the Alpha (between 0 and 2) to be used with the Successive Relaxation Method=');
end
disp('==============================================================================')
ZL=1i*Freq*L;
ZC=1./(1i*Freq*C);
%empty matrix of size (n+1 x n+1) with all zeros
%Z=impedance
Z=zeros(n+1,n+1);
%This loop to construct every thing in the matrix exept the first & last
%rows.
%odd for capacitors,even for resistors.
for k=2:n
%using counter
if(mod (k,2)==0)
Z(k,k)= ZL(k-1)+R(k/2)+ZL(k);
Z(k,k-1)= -ZL(k-1);
Z(k,k+1)= -ZL(k);
%Regarding the capacitors:
else
Z(k,k)=ZL(k-1)+ZC((k+1)/2)+ZL(k);
Z(k,k-1)= -ZL(k-1);
Z(k,k+1)=-ZL(k);
end
end
%Row1, first entery.
Z(1,1)= ZC(1)+ ZL(1);
%Row1, second enetry.
Z(1,2)= -ZL(1);
%Last row.
Z(n+1,n+1)= ZL(n)+R((n+1)/2);%Or R((n+1)/2)+ ZL(end)
Z(n+1,n)= -ZL(n); %or -ZL(end)
disp('The impedance matrix to solve the mesh current is:');
disp(Z)
V=zeros((n+1),1);
V(1,1)=abs(Vmag).*(cosd(Vphase)+1i*sind(Vphase));
disp('The voltage Vector to solve the mesh current is:');
disp(V)
disp('============================================================');
I=Z\V; %inverse of Z*V , not the same as V/Z
x=zeros(1,n);
error=2000;
while (error>0.001)
xprev = x;
for(n=1:n+1)
j =[[1:(n-1)] [(n+1):n]];
x(n) = (1-Alpha)*x(n) + (Alpha/Z(n,n)) * (V(n) - sum(Z(n,j).*x(j)));
end
end
  2 Comments
Rula laith
Rula laith on 17 Apr 2021
I’m sorry , the problem started at V=zeros((n+1),1);
This is a sample
Enter the inductors values in Henry, [L1...Ln]=[0.2 0.3] The number of inductors should be odd. Re-enter values of the inductors in Henry, [L1...Ln]=[0.2 0.4 0.1] ============================================================================== Enter the resistance values in Ohm, [R1...R(n+1)/2]=[10 30 20] The number of resistor should be 2, i.e. 1/2 (number of inductors + 1) Re-enter the resistance values in Ohm, [R1...R(n+1)/2]=[10 30] ============================================================================== Enter the capacitance values in Farads, [C1...C(n+1)/2]=[0.01 0.02 0.04] The number of capacitors should be 2, 1/2 (number of inductors + 1) Re-enter the capacitance values in Farads, [C1...C(n+1)/2]=[0.01 0.02] ============================================================================== Enter the magnitude of the voltage sources in volt, Vmag=[10 20] ============================================================================== Enter the phase of the voltage sources respectively in degree, Vphase=[0 0] ============================================================================== Enter the frequency of the voltage source in rad/s, Freq=100 ============================================================================== Enter the Alpha (between 0 and 2) to be used with the Successive Relaxation Method=-2 The Alpha should be positive real number between (0 and 2), check the value and enter it again Enter the Alpha (between 0 and 2) to be used with the Successive Relaxation Method=5 The Alpha should be positive real number between (0 and 2), check the value and enter it again Enter the Alpha (between 0 and 2) to be used with the Successive Relaxation Method=0.5 ============================================================================== The impedance matrix to solve the mesh current is: 0.0000 +19.0000i 0.0000 -20.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 -20.0000i 10.0000 +60.0000i 0.0000 -40.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 -40.0000i 0.0000 +49.5000i 0.0000 -10.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 -10.0000i 30.0000 +10.0000i The voltage vector to solve the mesh current is: 10 0 0 -20 ============================================================================== The matrix is not strictly diagonally dominant at row 1 The matrix is not strictly diagonally dominant at row 3 approximated, 0.45234-1.1937i 0.42972-0.63574i 0.25939-0.46625i exact= 0.46293-1.2005i 0.43978-0.64047i 0.26809-0.4704i -0.43207+0.23339i -0.43441+0.23127i ============================================================================== relative error, 0.0097978 0.014308 significant digit= 2 2
0.017796 2 0.00643 2 ============================================================================== Magnitude Phase 1.2765 -69.2457 0.7674 -55.9441 0.5336 -60.9113 0.4921 151.9703 =====================================================================

Sign in to comment.

Answers (2)

Clayton Gotberg
Clayton Gotberg on 16 Apr 2021
Edited: Clayton Gotberg on 16 Apr 2021
If the error says "Unable to perform assignment because the left and right sides have a different number of elements" the issue is that somewhere you are trying to put something where it does not fit.
For example,
A = [1 2 3 4 5];
B = [6 7];
A(5) = B;
Will give the same error because B is 1x2 but the space specified for it (position 5 of A) is only 1x1.
Similarly,
A(3:5) = B;
Gives the same error because now the space specified for it is 3x1. Wherever the error is, it is because you have specified how much space something is supposed to take up on both sides of an equation and the two sides disagree.
It's tedious to help you narrow down the precise location because we don't know the shape of the arrays you start with (the ones your program asks you to enter each time). You can troubleshoot this yourself by running the code one line or section at a time until you determine where the error is happening.
I ran your code with some random values though, and it says the error is that "Index exceeds number of array elements" which means that, somewhere, you are asking for an element that does not exist in the array (for example, asking for A(20) above). If this is your problem, it's happening in the last part, where you have a while loop and a for loop, because you are using n in both parts of the for loop.
x=zeros(1,n);
error=2000;
while (error>0.001)
xprev = x;
for(n=1:n+1) % This is the line where the problem starts.
j =[[1:(n-1)] [(n+1):n]];
x(n) = (1-Alpha)*x(n) + (Alpha/Z(n,n)) * (V(n) - sum(Z(n,j).*x(j)));
end
end
The problem with this structure is that you are using n to tell the for loop when to stop, but also changing n on each pass. Essentially, you are moving the finish line at the end of every iteration.
The solution is to let n stay constant by using another variable in the loop. For example,
for(loop_var=1:n+1)
j =[[1:(loop_var-1)] [(loop_var+1):loop_var]];
x(loop_var) = (1-Alpha)*x(loop_var) + (Alpha/Z(loop_var,loop_var)) * (V(loop_var) - sum(Z(loop_var,j).*x(j)));
end
I hope this helps! Please be sure to include the exact wording of the error next time so that everyone can assist you as quickly and easily as possible.
  2 Comments

Sign in to comment.


Walter Roberson
Walter Roberson on 17 Apr 2021
V = abs(Vmag(:)).*(cosd(Vphase(:))+1i*sind(Vphase(:)));
V(end+1:n+1,1) = 0;
You will need to recheck this logic.
You also need to fix up x
x = zeros(1,n+1);
error=2000;
while (error>0.001)
xprev = x;
for N=1:n+1
j = [(1:(N-1)) ((N+1):n)];
x(N) = (1-Alpha)*x(N) + (Alpha/Z(N,N)) * (V(N) - sum(Z(N,j).*x(j)));
end
end
You previously had for n=1:n+1 and after that you were confusing the n that is the length, with the n that is the for loop index.
You need to recheck which of the N (if any) should be the length instead of the loop index.
You also need to fix the code so that you change error as you currently have an infinite loop.
It is a good idea to put a limit on the number of iterations, in case somehow it diverges instead of converging.
It is not good practice to use error as the name of a variable, as that interferes with using the important MATLAB function named error()
  2 Comments
Walter Roberson
Walter Roberson on 17 Apr 2021
while (error>0.001)
xprev = x;
for N=1:n+1
j = [(1:(N-1)) ((N+1):n)];
x(N) = (1-Alpha)*x(N) + (Alpha/Z(N,N)) * (V(N) - sum(Z(N,j).*x(j)));
end
end
You have a while loop. while loops can proceed several different ways:
  • the condition might always be false, so the body might not be executed
  • the condition might be based on variables that the body always changes to force the condition to be false, in which case the body would execute exactly once
  • the condition test might be based on variables that only get modified in on some paths but not other paths. In this case, the body might repeat a number of times, and there might be accidents or oversight in coding (such as not taking into account abs() or round-off error) that cause the variables to never be modified, in which case it will repeat infinitely
  • the condition test might be based on variables that never get modified, in which case the while will repeat infinitely

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!