it says the right side does not equal to the left i dont know what should i do can anyone help me pls?
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
Walter Roberson
il 16 Apr 2021
You did not give us sample values to test with, and you did not indicate which line the problem occurs on :(
Risposte (2)
Clayton Gotberg
il 16 Apr 2021
Modificato: Clayton Gotberg
il 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.
Walter Roberson
il 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 Commenti
Walter Roberson
il 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
Vedere anche
Categorie
Scopri di più su Linear Algebra in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!