function fig = symbolicMathApp
% SYMBOLICMATHAPP Explore single-variable expressions with Symbolic Math Toolbox.
fig = uifigure('Name','数式ラボ — Symbolic Math Toolbox', ...
    'Position',[100 100 1060 720]);
layout = uigridlayout(fig,[7 1]);
layout.RowHeight = {40,32,32,32,90,'1x',24};
uilabel(layout,'Text','数式ラボ  |  微分・積分・方程式', ...
    'FontSize',23,'FontWeight','bold');
row = uigridlayout(layout,[1 3]);
row.ColumnWidth = {85,'1x',220}; row.Padding = [0 0 0 0];
uilabel(row,'Text','数式 f(x)');
expression = uieditfield(row,'text','Value','sin(x)*exp(-x/5)');
examples = uidropdown(row,'Items',{'減衰する波','多項式','ガウス関数'}, ...
    'ValueChangedFcn',@chooseExample);
row = uigridlayout(layout,[1 4]);
row.ColumnWidth = {85,220,'1x',120}; row.Padding = [0 0 0 0];
uilabel(row,'Text','操作');
operation = uidropdown(row,'Items',{'微分','不定積分','簡約','方程式 f(x)=0'});
uilabel(row,'Text','変数は x のみ。例: x^3-3*x、sin(x)');
button = uibutton(row,'Text','計算する','ButtonPushedFcn',@calculate);
row = uigridlayout(layout,[1 5]);
row.ColumnWidth = {85,110,35,110,'1x'}; row.Padding = [0 0 0 0];
uilabel(row,'Text','描画範囲');
lower = uieditfield(row,'numeric','Value',-5);
uilabel(row,'Text','〜');
upper = uieditfield(row,'numeric','Value',5);
uilabel(row,'Text','方程式の解は描画範囲に限定されません');
result = uitextarea(layout,'Editable','off','FontSize',15);
ax = uiaxes(layout); grid(ax,'on'); xlabel(ax,'x'); ylabel(ax,'y');
status = uilabel(layout,'Text','');
calculate();

    function chooseExample(~,~)
        switch examples.Value
            case '減衰する波'
                expression.Value = 'sin(x)*exp(-x/5)';
            case '多項式'
                expression.Value = 'x^3-3*x';
            case 'ガウス関数'
                expression.Value = 'exp(-x^2)';
        end
        calculate();
    end

    function calculate(~,~)
        button.Enable = 'off';
        status.Text = '計算中…'; drawnow;
        cleanup = onCleanup(@() set(button,'Enable','on'));
        try
            if ~isfinite(lower.Value) || ~isfinite(upper.Value) || lower.Value >= upper.Value
                error('SymbolicLab:Range','描画範囲は有限の数値で、左端 < 右端にしてください。');
            end
            x = sym('x');
            f = str2sym(expression.Value);
            if ~isscalar(f) || hasSymType(f,'equation') || ...
                    any(string(symvar(f)) ~= "x")
                error('SymbolicLab:Expression','変数 x だけを含む単一の式を入力してください（等号は不要）。');
            end
            switch operation.Value
                case '微分'
                    g = diff(f,x);
                    output = ['微分: ',char(g)];
                case '不定積分'
                    g = int(f,x);
                    output = ['不定積分: ',char(g),' + C'];
                case '簡約'
                    g = simplify(f);
                    output = ['簡約: ',char(g)];
                otherwise
                    g = [];
                    [solutions,parameters,conditions] = solve(f == 0,x,'ReturnConditions',true);
                    output = ['解: ',char(solutions),newline, ...
                        'パラメーター: ',char(parameters),newline,'条件: ',char(conditions)];
                    if isempty(solutions)
                        output = '解が返されませんでした。解なし、または記号的に求解できない可能性があります。';
                    end
            end
            result.Value = splitlines(string(output));
            cla(ax); hold(ax,'on');
            plotCleanup = onCleanup(@() hold(ax,'off'));
            fplot(ax,f,[lower.Value upper.Value],'LineWidth',2,'DisplayName','f(x)');
            if ~isempty(g)
                fplot(ax,g,[lower.Value upper.Value],'LineWidth',2,'DisplayName','計算結果（積分は C=0）');
            end
            yline(ax,0,':','HandleVisibility','off');
            legend(ax,'show','Location','best'); grid(ax,'on');
            title(ax,expression.Value,'Interpreter','none');
            clear plotCleanup;
            status.Text = '完了。グラフは実数値の部分を表示します。';
        catch exception
            status.Text = '入力または計算を確認してください。';
            result.Value = {exception.message};
        end
        clear cleanup;
    end
end
