Mohr's Circle Before Trying

 avatar
itsLu
matlab
a month ago
22 kB
1
Indexable
Never
classdef MohrCircle < matlab.apps.AppBase

    % Properties that correspond to app components
    properties (Access = public)
        UIFigure                     matlab.ui.Figure
        StressStrainCalculatorPanel  matlab.ui.container.Panel
        CalculateParametersDrawCircleButton  matlab.ui.control.Button
        InputparametersPanel         matlab.ui.container.Panel
        RotationAngleEditField       matlab.ui.control.NumericEditField
        RotationAngleLabel           matlab.ui.control.Label
        ShearEditField               matlab.ui.control.NumericEditField
        ShearLabel                   matlab.ui.control.Label
        Sigma_yEditField             matlab.ui.control.NumericEditField
        Sigma_yEditFieldLabel        matlab.ui.control.Label
        Sigma_xEditField             matlab.ui.control.NumericEditField
        Sigma_xEditFieldLabel        matlab.ui.control.Label
        OutputparametersPanel        matlab.ui.container.Panel
        Theta_s2EditField            matlab.ui.control.NumericEditField
        Theta_s2EditFieldLabel       matlab.ui.control.Label
        Theta_s1EditField            matlab.ui.control.NumericEditField
        Theta_s1EditFieldLabel       matlab.ui.control.Label
        MaximumShearEditField        matlab.ui.control.NumericEditField
        MaximumShearEditFieldLabel   matlab.ui.control.Label
        MaximumShearStressLabel      matlab.ui.control.Label
        Theta_p2EditField            matlab.ui.control.NumericEditField
        Theta_p2EditFieldLabel       matlab.ui.control.Label
        Theta_p1EditField            matlab.ui.control.NumericEditField
        Theta_p1EditFieldLabel       matlab.ui.control.Label
        Sigma2EditField              matlab.ui.control.NumericEditField
        Sigma2EditFieldLabel         matlab.ui.control.Label
        Sigma1EditField              matlab.ui.control.NumericEditField
        Sigma1EditFieldLabel         matlab.ui.control.Label
        PrincipalStressesLabel       matlab.ui.control.Label
        RotationstressesLabel        matlab.ui.control.Label
        Shear_xyEditField            matlab.ui.control.NumericEditField
        Shear_xyEditFieldLabel       matlab.ui.control.Label
        Sigma_y1EditField            matlab.ui.control.NumericEditField
        Sigma_y1EditFieldLabel       matlab.ui.control.Label
        Sigma_x1EditField            matlab.ui.control.NumericEditField
        Sigma_x1EditFieldLabel       matlab.ui.control.Label
        UIAxes                       matlab.ui.control.UIAxes
    end

    % Callbacks that handle component events
    methods (Access = private)

        % Button pushed function: CalculateParametersDrawCircleButton
        function CalculateParametersDrawCircleButtonPushed(app, event)
            Sigma_x = app.Sigma_xEditField.Value; 
            Sigma_y = app.Sigma_yEditField.Value;
            Shear = app.ShearEditField.Value;
            RotationAngle = app.RotationAngleEditField.Value;
            
            Sigma_x1 = ((Sigma_x+Sigma_y)/2) + (((Sigma_x-Sigma_y)/2)*cosd(2*RotationAngle)) + (Shear*sind(2*RotationAngle));
             
            Sigma_y1 = ((Sigma_x+Sigma_y)/2) - (((Sigma_x-Sigma_y)/2)*cosd(2*RotationAngle)) - (Shear*sind(2*RotationAngle));

            Shear_xy = ((-(Sigma_x-Sigma_y)/2)*sind(2*RotationAngle)) + (Shear*cosd(2*RotationAngle));
            
            Sigma1 = ((Sigma_x+Sigma_y)/2) + sqrt((((Sigma_x-Sigma_y)/2)^2)+ Shear^2);

            Sigma2 = ((Sigma_x+Sigma_y)/2) - sqrt((((Sigma_x-Sigma_y)/2)^2)+ Shear^2);

            a=Sigma_x-Sigma_y;
            b=Shear;
            c=2*(b/a);
            d=atand(c);

            Theta_p1=d/2;
            Theta_p2 = Theta_p1+90;

            %Theta_p1 = (atan((2*Shear)/(Sigma_x-Sigma_y)))/2;

            %Theta_p2 = (atan((2*Shear)/(Sigma_x-Sigma_y)))/2 +90;
            
            MaximumShear = sqrt((((Sigma_x-Sigma_y)/2)^2)+ Shear^2);
            
            s = atand(-a/(2*b));
            Theta_s1 = s/2;

            %Theta_s = (atan(-(Sigma_x-Sigma_y)/(2*Shear)))/2;

            app.Sigma_x1EditField.Value = Sigma_x1;
            app.Sigma_y1EditField.Value = Sigma_y1;
            app.Shear_xyEditField.Value = Shear_xy;
            app.Sigma1EditField.Value = Sigma1;
            app.Sigma2EditField.Value = Sigma2;
            app.Theta_p1EditField.Value = Theta_p1;
            app.Theta_p2EditField.Value = Theta_p2;
            app.MaximumShearEditField.Value = MaximumShear;
            app.Theta_s1EditField.Value = Theta_s1;
               
            % Compung Radius and Center of the circle
            R=sqrt(((Sigma_x-Sigma_y)/2)^2+Shear^2);
            C=(Sigma_x+Sigma_y)/2;
            Sigma = linspace(Sigma2, Sigma1, 1001);
            
            % Plot both positive and negative shear stress on app.UIAxes
            plot(app.UIAxes, Sigma, Shear, 'b', Sigma, -Shear, 'b'); 
            
            % Customize axes labels and properties
            xlabel(app.UIAxes, 'Sigma');
            ylabel(app.UIAxes, 'Shear Stress');
            
            % Fix aspect ratio to ensure circle looks correct
            axis(app.UIAxes, 'equal');
            
            % Add diameters for principal stresses and initial stress state
            hold(app.UIAxes, 'on');
            Ax = Sigma_x;
            Ay = Shear;
            Bx = Sigma_y;
            By = -Shear;
            Cx = Sigma_x1;
            Cy = Shear_xy;
            Dx = Sigma_x1;
            Dy = Shear_xy;
            DDx = Sigma_y1;
            DDy = -Shear_xy;

            plot(app.UIAxes, [Ax, Bx], [Ay, By], 'r');
            %plot(app.UIAxes, [Bx, Cx], [By, Cy], 'r');
            plot(app.UIAxes, [Cx, Dx], [Cy, Dy], 'g');
            plot(app.UIAxes, [Dx, DDx], [Dy, DDy], 'g');

            %plot(app.UIAxes, [Sigma1, Sigma2], [0, 0], 'k');
            %plot(app.UIAxes, [Sigma_x, Sigma_y], [-Shear, Shear], 'r');
            %plot(app.UIAxes, [Sigma_x, Sigma_y], [Shear, -Shear], 'g');
            % Draw Mohr's circle
            viscircles(app.UIAxes, [C, 0], R, 'Color', 'r');
            
            % (Optional) Enable grid for better visualization
            grid(app.UIAxes, 'on');
            
            % Release the hold on app.UIAxes
            hold(app.UIAxes, 'off');


        end

        % Value changed function: Sigma_xEditField
        function Sigma_xEditFieldValueChanged(app, event)
           value = app.Sigma_xEditField.Value;
          %Sigma_x = app.Sigma_x1EditField.Value; 
        end

        % Value changed function: Sigma_yEditField
        function Sigma_yEditFieldValueChanged(app, event)
            value = app.Sigma_yEditField.Value;
            
        end

        % Value changed function: ShearEditField
        function ShearEditFieldValueChanged(app, event)
            value = app.ShearEditField.Value;
            
        end

        % Value changed function: RotationAngleEditField
        function RotationAngleEditFieldValueChanged(app, event)
            value = app.RotationAngleEditField.Value;
            
        end
    end

    % Component initialization
    methods (Access = private)

        % Create UIFigure and components
        function createComponents(app)

            % Create UIFigure and hide until all components are created
            app.UIFigure = uifigure('Visible', 'off');
            app.UIFigure.Position = [100 100 640 544];
            app.UIFigure.Name = 'MATLAB App';

            % Create StressStrainCalculatorPanel
            app.StressStrainCalculatorPanel = uipanel(app.UIFigure);
            app.StressStrainCalculatorPanel.TitlePosition = 'centertop';
            app.StressStrainCalculatorPanel.Title = 'Stress-Strain Calculator';
            app.StressStrainCalculatorPanel.Position = [25 13 596 516];

            % Create UIAxes
            app.UIAxes = uiaxes(app.StressStrainCalculatorPanel);
            title(app.UIAxes, 'Mohr''s Circle')
            xlabel(app.UIAxes, 'Sigma_x1')
            ylabel(app.UIAxes, 'y''')
            zlabel(app.UIAxes, 'Z')
            app.UIAxes.FontName = 'Calibri';
            app.UIAxes.YDir = 'reverse';
            app.UIAxes.XAxisLocation = 'origin';
            app.UIAxes.YAxisLocation = 'origin';
            app.UIAxes.Position = [11 8 300 260];

            % Create OutputparametersPanel
            app.OutputparametersPanel = uipanel(app.StressStrainCalculatorPanel);
            app.OutputparametersPanel.Title = 'Output parameters';
            app.OutputparametersPanel.FontName = 'Calibri';
            app.OutputparametersPanel.Position = [326 68 260 420];

            % Create Sigma_x1EditFieldLabel
            app.Sigma_x1EditFieldLabel = uilabel(app.OutputparametersPanel);
            app.Sigma_x1EditFieldLabel.HorizontalAlignment = 'right';
            app.Sigma_x1EditFieldLabel.FontName = 'Calibri';
            app.Sigma_x1EditFieldLabel.Position = [15 345 52 22];
            app.Sigma_x1EditFieldLabel.Text = 'Sigma_x1';

            % Create Sigma_x1EditField
            app.Sigma_x1EditField = uieditfield(app.OutputparametersPanel, 'numeric');
            app.Sigma_x1EditField.ValueDisplayFormat = '%.3f';
            app.Sigma_x1EditField.HorizontalAlignment = 'left';
            app.Sigma_x1EditField.FontName = 'Calibri';
            app.Sigma_x1EditField.Position = [82 345 100 22];

            % Create Sigma_y1EditFieldLabel
            app.Sigma_y1EditFieldLabel = uilabel(app.OutputparametersPanel);
            app.Sigma_y1EditFieldLabel.HorizontalAlignment = 'right';
            app.Sigma_y1EditFieldLabel.FontName = 'Calibri';
            app.Sigma_y1EditFieldLabel.Position = [15 314 52 22];
            app.Sigma_y1EditFieldLabel.Text = 'Sigma_y1';

            % Create Sigma_y1EditField
            app.Sigma_y1EditField = uieditfield(app.OutputparametersPanel, 'numeric');
            app.Sigma_y1EditField.ValueDisplayFormat = '%.3f';
            app.Sigma_y1EditField.HorizontalAlignment = 'left';
            app.Sigma_y1EditField.FontName = 'Calibri';
            app.Sigma_y1EditField.Position = [82 314 100 22];

            % Create Shear_xyEditFieldLabel
            app.Shear_xyEditFieldLabel = uilabel(app.OutputparametersPanel);
            app.Shear_xyEditFieldLabel.HorizontalAlignment = 'right';
            app.Shear_xyEditFieldLabel.FontName = 'Calibri';
            app.Shear_xyEditFieldLabel.Position = [18 284 49 22];
            app.Shear_xyEditFieldLabel.Text = 'Shear_xy';

            % Create Shear_xyEditField
            app.Shear_xyEditField = uieditfield(app.OutputparametersPanel, 'numeric');
            app.Shear_xyEditField.ValueDisplayFormat = '%.3f';
            app.Shear_xyEditField.HorizontalAlignment = 'left';
            app.Shear_xyEditField.FontName = 'Calibri';
            app.Shear_xyEditField.Position = [82 284 100 22];

            % Create RotationstressesLabel
            app.RotationstressesLabel = uilabel(app.OutputparametersPanel);
            app.RotationstressesLabel.FontName = 'Calibri';
            app.RotationstressesLabel.Position = [15 366 88 22];
            app.RotationstressesLabel.Text = 'Rotation stresses';

            % Create PrincipalStressesLabel
            app.PrincipalStressesLabel = uilabel(app.OutputparametersPanel);
            app.PrincipalStressesLabel.FontName = 'Calibri';
            app.PrincipalStressesLabel.Position = [11 254 89 22];
            app.PrincipalStressesLabel.Text = 'Principal Stresses';

            % Create Sigma1EditFieldLabel
            app.Sigma1EditFieldLabel = uilabel(app.OutputparametersPanel);
            app.Sigma1EditFieldLabel.HorizontalAlignment = 'right';
            app.Sigma1EditFieldLabel.FontName = 'Calibri';
            app.Sigma1EditFieldLabel.Position = [27 230 40 22];
            app.Sigma1EditFieldLabel.Text = 'Sigma1';

            % Create Sigma1EditField
            app.Sigma1EditField = uieditfield(app.OutputparametersPanel, 'numeric');
            app.Sigma1EditField.ValueDisplayFormat = '%.3f';
            app.Sigma1EditField.HorizontalAlignment = 'left';
            app.Sigma1EditField.FontName = 'Calibri';
            app.Sigma1EditField.Position = [82 230 100 22];

            % Create Sigma2EditFieldLabel
            app.Sigma2EditFieldLabel = uilabel(app.OutputparametersPanel);
            app.Sigma2EditFieldLabel.HorizontalAlignment = 'right';
            app.Sigma2EditFieldLabel.FontName = 'Calibri';
            app.Sigma2EditFieldLabel.Position = [27 199 40 22];
            app.Sigma2EditFieldLabel.Text = 'Sigma2';

            % Create Sigma2EditField
            app.Sigma2EditField = uieditfield(app.OutputparametersPanel, 'numeric');
            app.Sigma2EditField.ValueDisplayFormat = '%.3f';
            app.Sigma2EditField.HorizontalAlignment = 'left';
            app.Sigma2EditField.FontName = 'Calibri';
            app.Sigma2EditField.Position = [82 199 100 22];

            % Create Theta_p1EditFieldLabel
            app.Theta_p1EditFieldLabel = uilabel(app.OutputparametersPanel);
            app.Theta_p1EditFieldLabel.HorizontalAlignment = 'right';
            app.Theta_p1EditFieldLabel.FontName = 'Calibri';
            app.Theta_p1EditFieldLabel.Position = [16 169 51 22];
            app.Theta_p1EditFieldLabel.Text = 'Theta_p1';

            % Create Theta_p1EditField
            app.Theta_p1EditField = uieditfield(app.OutputparametersPanel, 'numeric');
            app.Theta_p1EditField.ValueDisplayFormat = '%.3f';
            app.Theta_p1EditField.HorizontalAlignment = 'left';
            app.Theta_p1EditField.FontName = 'Calibri';
            app.Theta_p1EditField.Position = [82 169 100 22];

            % Create Theta_p2EditFieldLabel
            app.Theta_p2EditFieldLabel = uilabel(app.OutputparametersPanel);
            app.Theta_p2EditFieldLabel.HorizontalAlignment = 'right';
            app.Theta_p2EditFieldLabel.FontName = 'Calibri';
            app.Theta_p2EditFieldLabel.Position = [16 139 51 22];
            app.Theta_p2EditFieldLabel.Text = 'Theta_p2';

            % Create Theta_p2EditField
            app.Theta_p2EditField = uieditfield(app.OutputparametersPanel, 'numeric');
            app.Theta_p2EditField.ValueDisplayFormat = '%.3f';
            app.Theta_p2EditField.HorizontalAlignment = 'left';
            app.Theta_p2EditField.FontName = 'Calibri';
            app.Theta_p2EditField.Position = [82 139 100 22];

            % Create MaximumShearStressLabel
            app.MaximumShearStressLabel = uilabel(app.OutputparametersPanel);
            app.MaximumShearStressLabel.FontName = 'Calibri';
            app.MaximumShearStressLabel.Position = [11 112 116 22];
            app.MaximumShearStressLabel.Text = 'Maximum Shear Stress';

            % Create MaximumShearEditFieldLabel
            app.MaximumShearEditFieldLabel = uilabel(app.OutputparametersPanel);
            app.MaximumShearEditFieldLabel.HorizontalAlignment = 'right';
            app.MaximumShearEditFieldLabel.FontName = 'Calibri';
            app.MaximumShearEditFieldLabel.Position = [22 88 85 22];
            app.MaximumShearEditFieldLabel.Text = 'Maximum Shear';

            % Create MaximumShearEditField
            app.MaximumShearEditField = uieditfield(app.OutputparametersPanel, 'numeric');
            app.MaximumShearEditField.ValueDisplayFormat = '%.3f';
            app.MaximumShearEditField.HorizontalAlignment = 'left';
            app.MaximumShearEditField.FontName = 'Calibri';
            app.MaximumShearEditField.Position = [122 88 100 22];

            % Create Theta_s1EditFieldLabel
            app.Theta_s1EditFieldLabel = uilabel(app.OutputparametersPanel);
            app.Theta_s1EditFieldLabel.HorizontalAlignment = 'right';
            app.Theta_s1EditFieldLabel.FontName = 'Calibri';
            app.Theta_s1EditFieldLabel.Position = [58 57 49 22];
            app.Theta_s1EditFieldLabel.Text = 'Theta_s1';

            % Create Theta_s1EditField
            app.Theta_s1EditField = uieditfield(app.OutputparametersPanel, 'numeric');
            app.Theta_s1EditField.ValueDisplayFormat = '%.3f';
            app.Theta_s1EditField.HorizontalAlignment = 'left';
            app.Theta_s1EditField.FontName = 'Calibri';
            app.Theta_s1EditField.Position = [122 57 100 22];

            % Create Theta_s2EditFieldLabel
            app.Theta_s2EditFieldLabel = uilabel(app.OutputparametersPanel);
            app.Theta_s2EditFieldLabel.HorizontalAlignment = 'right';
            app.Theta_s2EditFieldLabel.FontName = 'Calibri';
            app.Theta_s2EditFieldLabel.Position = [58 26 49 22];
            app.Theta_s2EditFieldLabel.Text = 'Theta_s2';

            % Create Theta_s2EditField
            app.Theta_s2EditField = uieditfield(app.OutputparametersPanel, 'numeric');
            app.Theta_s2EditField.ValueDisplayFormat = '%.3f';
            app.Theta_s2EditField.HorizontalAlignment = 'left';
            app.Theta_s2EditField.FontName = 'Calibri';
            app.Theta_s2EditField.Position = [122 26 100 22];

            % Create InputparametersPanel
            app.InputparametersPanel = uipanel(app.StressStrainCalculatorPanel);
            app.InputparametersPanel.Title = 'Input parameters';
            app.InputparametersPanel.FontName = 'Calibri';
            app.InputparametersPanel.FontWeight = 'bold';
            app.InputparametersPanel.Position = [22 310 260 178];

            % Create Sigma_xEditFieldLabel
            app.Sigma_xEditFieldLabel = uilabel(app.InputparametersPanel);
            app.Sigma_xEditFieldLabel.HorizontalAlignment = 'right';
            app.Sigma_xEditFieldLabel.FontName = 'Calibri';
            app.Sigma_xEditFieldLabel.Position = [73 123 45 22];
            app.Sigma_xEditFieldLabel.Text = 'Sigma_x';

            % Create Sigma_xEditField
            app.Sigma_xEditField = uieditfield(app.InputparametersPanel, 'numeric');
            app.Sigma_xEditField.ValueChangedFcn = createCallbackFcn(app, @Sigma_xEditFieldValueChanged, true);
            app.Sigma_xEditField.HorizontalAlignment = 'left';
            app.Sigma_xEditField.FontName = 'Calibri';
            app.Sigma_xEditField.Position = [133 123 100 22];

            % Create Sigma_yEditFieldLabel
            app.Sigma_yEditFieldLabel = uilabel(app.InputparametersPanel);
            app.Sigma_yEditFieldLabel.HorizontalAlignment = 'right';
            app.Sigma_yEditFieldLabel.FontName = 'Calibri';
            app.Sigma_yEditFieldLabel.Position = [72 87 46 22];
            app.Sigma_yEditFieldLabel.Text = 'Sigma_y';

            % Create Sigma_yEditField
            app.Sigma_yEditField = uieditfield(app.InputparametersPanel, 'numeric');
            app.Sigma_yEditField.ValueChangedFcn = createCallbackFcn(app, @Sigma_yEditFieldValueChanged, true);
            app.Sigma_yEditField.HorizontalAlignment = 'left';
            app.Sigma_yEditField.FontName = 'Calibri';
            app.Sigma_yEditField.Position = [133 87 100 22];

            % Create ShearLabel
            app.ShearLabel = uilabel(app.InputparametersPanel);
            app.ShearLabel.HorizontalAlignment = 'right';
            app.ShearLabel.FontName = 'Calibri';
            app.ShearLabel.Position = [72 47 47 22];
            app.ShearLabel.Text = 'Shear (τ)';

            % Create ShearEditField
            app.ShearEditField = uieditfield(app.InputparametersPanel, 'numeric');
            app.ShearEditField.ValueChangedFcn = createCallbackFcn(app, @ShearEditFieldValueChanged, true);
            app.ShearEditField.HorizontalAlignment = 'left';
            app.ShearEditField.FontName = 'Calibri';
            app.ShearEditField.Position = [134 47 100 22];

            % Create RotationAngleLabel
            app.RotationAngleLabel = uilabel(app.InputparametersPanel);
            app.RotationAngleLabel.HorizontalAlignment = 'right';
            app.RotationAngleLabel.FontName = 'Calibri';
            app.RotationAngleLabel.Position = [26 15 93 22];
            app.RotationAngleLabel.Text = 'Rotation Angle (θ)';

            % Create RotationAngleEditField
            app.RotationAngleEditField = uieditfield(app.InputparametersPanel, 'numeric');
            app.RotationAngleEditField.ValueChangedFcn = createCallbackFcn(app, @RotationAngleEditFieldValueChanged, true);
            app.RotationAngleEditField.HorizontalAlignment = 'left';
            app.RotationAngleEditField.FontName = 'Calibri';
            app.RotationAngleEditField.Position = [134 15 100 22];

            % Create CalculateParametersDrawCircleButton
            app.CalculateParametersDrawCircleButton = uibutton(app.StressStrainCalculatorPanel, 'push');
            app.CalculateParametersDrawCircleButton.ButtonPushedFcn = createCallbackFcn(app, @CalculateParametersDrawCircleButtonPushed, true);
            app.CalculateParametersDrawCircleButton.FontName = 'Calibri';
            app.CalculateParametersDrawCircleButton.Position = [58 278 187 23];
            app.CalculateParametersDrawCircleButton.Text = 'Calculate Parameters & Draw Circle';

            % Show the figure after all components are created
            app.UIFigure.Visible = 'on';
        end
    end

    % App creation and deletion
    methods (Access = public)

        % Construct app
        function app = MohrCircle

            % Create UIFigure and components
            createComponents(app)

            % Register the app with App Designer
            registerApp(app, app.UIFigure)

            if nargout == 0
                clear app
            end
        end

        % Code that executes before app deletion
        function delete(app)

            % Delete UIFigure when app is deleted
            delete(app.UIFigure)
        end
    end
end
Leave a Comment