2. (New) Create a script for the following problem. An engineer had measured imp
ID: 2249153 • Letter: 2
Question
2. (New) Create a script for the following problem. An engineer had measured impedances of a collection of electrical components and created a text file- impedance. data. You will find that impedance.txt on Canvas. The first Row of the file is MAGNITUDE of the impedance and the second Row is the PHASE (in radian) of the impedance. Column number indicates the number of components txt - based on the numerical Here are the needed conditions. (1) will read the impedance.txt and find the Column number (You can use size' function) (2) compute real part and imaginary part of impedance of individual component. (3) Save the result on a variable Impedance Catesian. 4) Save the result on a text file Impedance Cartesian.txt (5) Also have this following message displayed on the screen ("There are ### components in the file impedance.txt. The Conversion of POLAR format of complex number into CARTESIAN format is completed and the result is saved on Impedance Cartesian.txt") (6) Do this with FOR LOOP OR WHILE LOOP (Only a single script is needed!) Conversion Equation from Polar into Cartesian From MAGNITUDE 2 PHASE ANGLE-REAL +jIMAGINARY REAL = MAG * cos( PHASE ANGLE) IMAGINARY= MAGNITDE * sin(PHASE ANGLE) is not Convolution. It is product!Explanation / Answer
Please find below the MATLAB code with comments specified inline explaining each and every step.
The solution contains three ways (1) Directly operating on matrices, (2) Using 'For Loop' and (3) Using 'While Loop'. Each time we are printing the result and storing the output Impedance_Cartesian to a text file.
%-------Solution STARTS--------%
clear;
load('impedance.txt'); %first load the text file that contains the impedance values in Polar format
%---------Question1.1-----------%
Number_of_components=size(impedance,2); %The number of components=Number of columns in the variable 'impedance'->impedance.txt file
%---------Question1.2-----------%
Impedance_Catesian=zeros(size(impedance)); %Initate the variable Impedance_Catesian which contains Real and Imaginary parts of impedance of all components to zero
Real=impedance(1,:).*cos(impedance(2,:));
Imaginary=impedance(1,:).*sin(impedance(2,:));
%--------Question1.3-----------%
Impedance_Catesian(1,:) = Real;
Impedance_Catesian(2,:) = Imaginary;
Impedance_Catesian %Displays the Result Impedance in Cartesian format obtained using directly the operations on matrices
%--------Question1.4----------%
save('Impedance_Catesian.txt','Impedance_Catesian','-ASCII');
%--------Question1.5----------%
display(['"There are ', num2str(Number_of_components), ' components in the file impedance.txt. The Conversion of Polar format of complex number into Catesian is completed and the result is saved in Impedance_Catesian.txt"']);
%--------Question1.6---------%
%---------Using For loop---------%
load('impedance.txt'); %first load the text file that contains the impedance values
Number_of_components=size(impedance,2); %The number of components=Number of columns in the variable 'impedance'->impedance.txt file
Impedance_Catesian=zeros(size(impedance)); %Initate the variable Impedance_Catesian which contains Real and Imaginary parts of impedance of all components to zero
Real=zeros(1,Number_of_components);
Imaginary=zeros(1,Number_of_components);
for i=1:Number_of_components
Real(1,i)=impedance(1,i)*cos(impedance(2,i));
Imaginary(1,i)=impedance(1,i)*sin(impedance(2,i));
end
Impedance_Catesian(1,:) = Real;
Impedance_Catesian(2,:) = Imaginary;
Impedance_Catesian %Displays the Result Impedance in Cartesian format obtained using 'For Loop'
save('Impedance_Catesian.txt','Impedance_Catesian','-ASCII');
display(['"There are ', num2str(Number_of_components), ' components in the file impedance.txt. The Conversion of Polar format of complex number into Catesian is completed and the result is saved in Impedance_Catesian.txt"']);
%---------Using While loop---------%
load('impedance.txt'); %first load the text file that contains the impedance values
Number_of_components=size(impedance,2); %The number of components=Number od rows in the variable 'impedance'->impedance.txt file
Impedance_Catesian=zeros(size(impedance)); %Initate the variable Impedance_Catesian which contains Real and Imaginary parts of impedance of all components to zero
Real=zeros(1,Number_of_components);
Imaginary=zeros(1,Number_of_components);
i=1;
while(i<=Number_of_components)
Real(1,i)=impedance(1,i)*cos(impedance(2,i));
Imaginary(1,i)=impedance(1,i)*sin(impedance(2,i));
i=i+1;
end
Impedance_Catesian(1,:) = Real;
Impedance_Catesian(2,:) = Imaginary;
Impedance_Catesian %Displays the Result Impedance in Cartesian format obtained using 'While Loop'
save('Impedance_Catesian.txt','Impedance_Catesian','-ASCII');
display(['"There are ', num2str(Number_of_components), ' components in the file impedance.txt. The Conversion of Polar format of complex number into Catesian is completed and the result is saved in Impedance_Catesian.txt"']);
%-------Solution END-------%
Please comment for any clarification
Thanks
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.