Untitled

 avatar
unknown
plain_text
a month ago
2.1 kB
2
Indexable
function log_decrement_gui
    % Create the main figure
    fig = figure('Position', [500, 200, 500, 400], 'Name', 'Logarithmic Decrement GUI', 'NumberTitle', 'off');

    % Labels and Input Fields
    uicontrol('Style', 'text', 'Position', [50, 320, 100, 20], 'String', 'Xo:', 'HorizontalAlignment', 'right');
    x0Field = uicontrol('Style', 'edit', 'Position', [160, 320, 200, 25], 'String', '-2');

    uicontrol('Style', 'text', 'Position', [50, 270, 100, 20], 'String', 'Xn:', 'HorizontalAlignment', 'right');
    xnField = uicontrol('Style', 'edit', 'Position', [160, 270, 200, 25], 'String', '0.8');

    uicontrol('Style', 'text', 'Position', [50, 220, 100, 20], 'String', 'N:', 'HorizontalAlignment', 'right');
    nField = uicontrol('Style', 'edit', 'Position', [160, 220, 200, 25], 'String', '100');

    % Calculate Button
    uicontrol('Style', 'pushbutton', 'Position', [200, 170, 100, 30], 'String', 'Calculate', ...
              'Callback', @(src, event) calculateValues());

    % Axes for Plot
    ax = axes('Parent', fig, 'Position', [0.1, 0.1, 0.8, 0.35]);

    % Callback function for calculation and plotting
    function calculateValues()
        % Get input values
        x0 = str2double(x0Field.String);
        xn = str2double(xnField.String);
        N = str2double(nField.String);

        % Validate inputs
        if isnan(x0) || isnan(xn) || isnan(N) || N <= 0 || x0 >= xn
            errordlg('Please enter valid values: Xo < Xn and N > 0.', 'Input Error');
            return;
        end

        % Generate x values and compute logarithmic decrement
        x = linspace(x0, xn, N);
        y = log(abs(x)); % Logarithmic values

        % Plot the logarithmic decrement graph
        cla(ax); % Clear previous plot
        plot(ax, x, y, 'b-', 'LineWidth', 1.5);
        grid(ax, 'on');
        title(ax, 'Logarithmic Decrement');
        xlabel(ax, 'x');
        ylabel(ax, 'log(|x|)');

        % Display results in the MATLAB command window
        fprintf('Calculated Values:\n');
        fprintf('x: %s\n', mat2str(x, 4));
        fprintf('y: %s\n', mat2str(y, 4));
    end
end
Leave a Comment