How to take a "differential equation" as input from user in matlab appdesigner?
7 views (last 30 days)
Show older comments
hi,
I am working on a app where user will insert a differential equation with initial conditions and the output will be the solution of the differential equation.I am confused which edit field to use to accept the differential equation from user.
let the equation,
y"+3y'+2y=sin(t)
y(0)=0,y'(0)=1
kindly share the code to solve this problem.thank you.
In matlab,this is the code how i solve the problem(the method is called EXPLICIT SOLUTION).But how to take this DE as input in appdesigner?/
eqn='D2y+3*Dy+2*y=sin(t)';
init='y(0)=0,Dy(0)=0';
y=dsolve(eqn,init)
last line returns the solution of this differential equation.
1 Comment
Walter Roberson
on 4 May 2022
Please note that that form of dsolve() is going to be discontinued soon. You should be constructing symbolic expressions using diff() . In turn you might use str2sym() on a quoted string. For example
eqn = str2sym('diff(y(t),t,t) + 3 * diff(y(t),t) + 2*y(t) == sin(t)')
dsolve(eqn)
Answers (1)
Alberto Cuadra Lara
on 4 May 2022
Edited: Alberto Cuadra Lara
on 4 May 2022
Hi Himalay,
As @Walter Roberson commented, support of character vectors and strings will be removed in a future release. Anyway, here's an app with that format implemented. You should adapt it to the new format for compatibility. I have included also the option to plot the solution of the ODE for a given range.
Best,
Alberto
![Snapshot](https://www.mathworks.com/matlabcentral/answers/uploaded_files/987165/Snapshot.png)
classdef example_ODE_app < matlab.apps.AppBase
% Author: Alberto Cuadra-Lara
% PhD Candidate - Dpto. Ingeniería Térmica y de Fluidos
% Office: 1.1.D23, Universidad Carlos III de Madrid
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
GridLayout matlab.ui.container.GridLayout
range matlab.ui.control.EditField
RangeEditFieldLabel matlab.ui.control.Label
solution matlab.ui.control.TextArea
SolutionTextAreaLabel matlab.ui.control.Label
initial_conditions matlab.ui.control.EditField
InitialconditionsEditFieldLabel matlab.ui.control.Label
ode matlab.ui.control.EditField
OrdDifferentialequationLabel matlab.ui.control.Label
PlotButton matlab.ui.control.Button
ClearButton matlab.ui.control.Button
CalculateButton matlab.ui.control.Button
end
properties (Access = private)
ode_solution % ODE solution [symbolic]
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: CalculateButton
function CalculateButtonPushed(app, event)
% Get inputs
eqn = app.ode.Value;
init = app.initial_conditions.Value;
% Solve ODE
app.ode_solution = dsolve(eqn, init);
% Save results in Solution object
if length(app.ode_solution) == 1
app.solution.Value = char(app.ode_solution);
else
app.solution.Value = ['[', strjoin(arrayfun(@char, app.ode_solution, 'uniform', 0),', '), ']'];
end
end
% Button pushed function: ClearButton
function ClearButtonPushed(app, event)
app.ode.Value = '';
app.initial_conditions.Value = '';
app.solution.Value = '';
end
% Button pushed function: PlotButton
function PlotButtonPushed(app, event)
% Get range
interval = app.range.Value;
% Set range
interval = sscanf(interval, '[%f,%f]');
% Miscellaneous
label_x = char(symvar(app.ode_solution));
% Plot range
try
fplot(app.ode_solution, [interval(1), interval(2)], 'LineWidth', 1.2);
set(gca, 'LineWidth', 1.2, 'FontSize', 14, 'BoxStyle', 'full')
xlabel(label_x, 'Interpreter', 'latex', 'FontSize', 18);
ylabel('y', 'Interpreter', 'latex', 'FontSize', 18);
catch
message = 'There was an error. Please specify the range as [%f,%f].';
uialert(app.UIFigure, message, 'Error', 'Icon', 'error');
end
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 = [700 400 403 232];
app.UIFigure.Name = 'MATLAB App';
% Create GridLayout
app.GridLayout = uigridlayout(app.UIFigure);
app.GridLayout.ColumnWidth = {48, 42, 49, 108, 100};
app.GridLayout.RowHeight = {22, 22, 22, '1x', 22, '1.06x', 22};
app.GridLayout.ColumnSpacing = 9.33333333333333;
app.GridLayout.RowSpacing = 11.125;
app.GridLayout.Padding = [9.33333333333333 11.125 9.33333333333333 11.125];
% Create CalculateButton
app.CalculateButton = uibutton(app.GridLayout, 'push');
app.CalculateButton.ButtonPushedFcn = createCallbackFcn(app, @CalculateButtonPushed, true);
app.CalculateButton.Layout.Row = 3;
app.CalculateButton.Layout.Column = 4;
app.CalculateButton.Text = 'Calculate';
% Create ClearButton
app.ClearButton = uibutton(app.GridLayout, 'push');
app.ClearButton.ButtonPushedFcn = createCallbackFcn(app, @ClearButtonPushed, true);
app.ClearButton.Layout.Row = 3;
app.ClearButton.Layout.Column = 5;
app.ClearButton.Text = 'Clear';
% Create PlotButton
app.PlotButton = uibutton(app.GridLayout, 'push');
app.PlotButton.ButtonPushedFcn = createCallbackFcn(app, @PlotButtonPushed, true);
app.PlotButton.Layout.Row = 7;
app.PlotButton.Layout.Column = 5;
app.PlotButton.Text = 'Plot';
% Create OrdDifferentialequationLabel
app.OrdDifferentialequationLabel = uilabel(app.GridLayout);
app.OrdDifferentialequationLabel.HorizontalAlignment = 'right';
app.OrdDifferentialequationLabel.Layout.Row = 1;
app.OrdDifferentialequationLabel.Layout.Column = [1 3];
app.OrdDifferentialequationLabel.Text = 'Ord. Differential equation';
% Create ode
app.ode = uieditfield(app.GridLayout, 'text');
app.ode.Layout.Row = 1;
app.ode.Layout.Column = [4 5];
app.ode.Value = 'D2y+3*Dy+2*y=sin(t)';
% Create InitialconditionsEditFieldLabel
app.InitialconditionsEditFieldLabel = uilabel(app.GridLayout);
app.InitialconditionsEditFieldLabel.HorizontalAlignment = 'right';
app.InitialconditionsEditFieldLabel.Layout.Row = 2;
app.InitialconditionsEditFieldLabel.Layout.Column = [2 3];
app.InitialconditionsEditFieldLabel.Text = 'Initial conditions';
% Create initial_conditions
app.initial_conditions = uieditfield(app.GridLayout, 'text');
app.initial_conditions.Layout.Row = 2;
app.initial_conditions.Layout.Column = [4 5];
app.initial_conditions.Value = 'y(0)=0,Dy(0)=0';
% Create SolutionTextAreaLabel
app.SolutionTextAreaLabel = uilabel(app.GridLayout);
app.SolutionTextAreaLabel.HorizontalAlignment = 'right';
app.SolutionTextAreaLabel.Layout.Row = 5;
app.SolutionTextAreaLabel.Layout.Column = 3;
app.SolutionTextAreaLabel.Text = 'Solution';
% Create solution
app.solution = uitextarea(app.GridLayout);
app.solution.Layout.Row = [4 6];
app.solution.Layout.Column = [4 5];
% Create RangeEditFieldLabel
app.RangeEditFieldLabel = uilabel(app.GridLayout);
app.RangeEditFieldLabel.HorizontalAlignment = 'right';
app.RangeEditFieldLabel.Layout.Row = 7;
app.RangeEditFieldLabel.Layout.Column = 3;
app.RangeEditFieldLabel.Text = 'Range';
% Create range
app.range = uieditfield(app.GridLayout, 'text');
app.range.Layout.Row = 7;
app.range.Layout.Column = 4;
app.range.Value = '[0,50]';
% 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 = example_ODE_ap1
% 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
5 Comments
Walter Roberson
on 8 May 2022
There is no documented support for using character vectors for the equations or initial conditions, not since around r2018a or so. And that means that MATLAB is allowed to get confused when you try.
See Also
Categories
Find more on Develop uifigure-Based Apps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!