Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.0 kB
2
Indexable
Never
% Ask for user inputs
f = input('Enter working frequency in Hertz: ');
Rus = input('Enter Rus termination resistance value in Ohm: ');
n = input('Enter the number of components in the circuit: ');
types = cell(1,n);
values = zeros(1,n);
for i = 1:n
    types{i} = input(sprintf('Enter connection type for component %d (S for series, P for parallel): ', i), 's');
    values(i) = input(sprintf('Enter component value for component %d: ', i));
end

% Define ABCD matrices for components
Zc = @(C) 1/(j*2*pi*f*C);
Zl = @(L) j*2*pi*f*L;
ABCD_Rs = [1, Rus; 0, 1];
ABCD_Rp = [1, 0; 1/Rus, 1];
ABCD_Ls = [1, Zl(values(i)); 0, 1];
ABCD_Lp = [1, 0; 1/Zl(values(i)), 1];
ABCD_Cs = [1, 0; Zc(values(i)), 1];
ABCD_Cp = [1, 1/Zc(values(i)); 0, 1];

% Calculate total ABCD matrix
ABCD = eye(2);
for i = n:-1:1
    switch types{i}
        case 'S'
            switch class(ABCD)
                case 'double'
                    ABCD = ABCD_Ls(values(i)) * ABCD;
                case 'sym'
                    ABCD = ABCD_Ls(sym(values(i))) * ABCD;
            end
        case 'P'
            switch class(ABCD)
                case 'double'
                    ABCD = ABCD_Lp(values(i)) * ABCD;
                case 'sym'
                    ABCD = ABCD_Lp(sym(values(i))) * ABCD;
            end
    end
end
ABCD = ABCD_Rs * ABCD;
for i = 1:n
    switch types{i}
        case 'S'
            switch class(ABCD)
                case 'double'
                    ABCD = ABCD * ABCD_Cs(values(i)) * ABCD_Ls(values(i));
                case 'sym'
                    ABCD = ABCD * ABCD_Cs(sym(values(i))) * ABCD_Ls(sym(values(i)));
            end
        case 'P'
            switch class(ABCD)
                case 'double'
                    ABCD = ABCD * ABCD_Cp(values(i)) * ABCD_Lp(values(i));
                case 'sym'
                    ABCD = ABCD * ABCD_Cp(sym(values(i))) * ABCD_Lp(sym(values(i)));
            end
    end
end
ABCD = ABCD * ABCD_Rs;
disp('Total ABCD matrix:');
disp(ABCD);