How to take a "differential equation" as input from user in matlab appdesigner?

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 Commento

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)')
eqn = 
dsolve(eqn)
ans = 

Accedi per commentare.

Risposte (1)

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
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 Commenti

thank you so much @Alberto Cuadra Lara for you detailed code.I try to follow your code as it is but found a error message only in plotting section.it is saying"Specify a UIAxes handle as first argument."in flot,xlabel,ylabel line.(see below i have attached the error image)also i have share my code.only problem is in plot section rest calculate function works perfectly.
could you kindly tell where is the problem?
thank you in advance.
classdef app2 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
OrdDifferentialequationEditFieldLabel matlab.ui.control.Label
ode matlab.ui.control.EditField
Initialc
onditionsEditFieldLabel matlab.ui.control.Label
initial_conditions matlab.ui.control.EditField
CalculateButton matlab.ui.control.Button
ClearButton matlab.ui.control.Button
SolutionEditFieldLabel matlab.ui.control.Label
solution matlab.ui.control.EditField
PlotButton matlab.ui.control.Button
RangeEditFieldLabel matlab.ui.control.Label
range matlab.ui.control.EditField
UIAxes matlab.ui.control.UIAxes
end
properties (Access = private)
ode_solution % Description
end
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
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'UI Figure';
setAutoResize(app, app.UIFigure, true)
% Create OrdDifferentialequationEditFieldLabel
app.OrdDifferentialequationEditFieldLabel = uilabel(app.UIFigure);
app.OrdDifferentialequationEditFieldLabel.HorizontalAlignment = 'right';
app.OrdDifferentialequationEditFieldLabel.Position = [51 420 141 15];
app.OrdDifferentialequationEditFieldLabel.Text = 'Ord. Differential equation';
% Create ode
app.ode = uieditfield(app.UIFigure, 'text');
app.ode.Position = [207 416 303 22];
% Create InitialconditionsEditFieldLabel
app.InitialconditionsEditFieldLabel = uilabel(app.UIFigure);
app.InitialconditionsEditFieldLabel.HorizontalAlignment = 'right';
app.InitialconditionsEditFieldLabel.Position = [102 382 93 15];
app.InitialconditionsEditFieldLabel.Text = 'Initial conditions';
% Create initial_conditions
app.initial_conditions = uieditfield(app.UIFigure, 'text');
app.initial_conditions.Position = [210 378 300 22];
% Create CalculateButton
app.CalculateButton = uibutton(app.UIFigure, 'push');
app.CalculateButton.ButtonPushedFcn = createCallbackFcn(app, @CalculateButtonPushed, true);
app.CalculateButton.Position = [232 308 100 22];
app.CalculateButton.Text = 'Calculate';
% Create ClearButton
app.ClearButton = uibutton(app.UIFigure, 'push');
app.ClearButton.ButtonPushedFcn = createCallbackFcn(app, @ClearButtonPushed, true);
app.ClearButton.Position = [394 308 100 22];
app.ClearButton.Text = 'Clear';
% Create SolutionEditFieldLabel
app.SolutionEditFieldLabel = uilabel(app.UIFigure);
app.SolutionEditFieldLabel.HorizontalAlignment = 'right';
app.SolutionEditFieldLabel.Position = [128 250 50 15];
app.SolutionEditFieldLabel.Text = 'Solution';
% Create solution
app.solution = uieditfield(app.UIFigure, 'text');
app.solution.Position = [193 135 317 133];
% Create PlotButton
app.PlotButton = uibutton(app.UIFigure, 'push');
app.PlotButton.ButtonPushedFcn = createCallbackFcn(app, @PlotButtonPushed, true);
app.PlotButton.Position = [395 67 100 22];
app.PlotButton.Text = 'Plot';
% Create RangeEditFieldLabel
app.RangeEditFieldLabel = uilabel(app.UIFigure);
app.RangeEditFieldLabel.HorizontalAlignment = 'right';
app.RangeEditFieldLabel.Position = [154 70 42 15];
app.RangeEditFieldLabel.Text = 'Range';
% Create range
app.range = uieditfield(app.UIFigure, 'text');
app.range.Position = [211 66 100 22];
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Title');
xlabel(app.UIAxes, 'X');
ylabel(app.UIAxes, 'Y');
app.UIAxes.Position = [296 97 300 185];
end
end
methods (Access = public)
% Construct app
function app = app2()
% Create and configure 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
Hello Himalay,
That is a warning message. It is always recommended to specify the axes object. One option is to include an UIAxes in the app as follows
classdef example_ODE_app < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
ODEeducationalUIFigure matlab.ui.Figure
Panel matlab.ui.container.Panel
ode matlab.ui.control.EditField
OrdDifferentialequationLabel matlab.ui.control.Label
initial_conditions matlab.ui.control.EditField
InitialconditionsEditFieldLabel matlab.ui.control.Label
range matlab.ui.control.EditField
RangeEditFieldLabel matlab.ui.control.Label
solution matlab.ui.control.TextArea
SolutionTextAreaLabel matlab.ui.control.Label
PlotButton matlab.ui.control.Button
ClearButton matlab.ui.control.Button
CalculateButton matlab.ui.control.Button
UIAxes matlab.ui.control.UIAxes
end
properties (Access = private)
ode_solution % ODE solution [symbolic]
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
% Initialize UIAxes
set(app.UIAxes, 'LineWidth', 1.2, 'FontSize', 14, 'BoxStyle', 'full');
xlabel(app.UIAxes, '$t$', 'Interpreter', 'latex', 'FontSize', 16);
ylabel(app.UIAxes, '$y$', 'Interpreter', 'latex', 'FontSize', 16);
end
% Button pushed function: CalculateButton
function CalculateButtonPushed(app, event)
% Get input string and format to symbolic
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.UIAxes, app.ode_solution, [interval(1), interval(2)], 'LineWidth', 1.2);
set(gca, 'LineWidth', 1.2, 'FontSize', 14, 'BoxStyle', 'full')
xlabel(app.UIAxes, '$t$', 'Interpreter', 'latex', 'FontSize', 16);
ylabel(app.UIAxes, '$y$', 'Interpreter', 'latex', 'FontSize', 16);
catch
message = 'There was an error. Please specify the range as [%f,%f].';
uialert(app.ODEeducationalUIFigure, message, 'Error', 'Icon', 'error');
end
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create ODEeducationalUIFigure and hide until all components are created
app.ODEeducationalUIFigure = uifigure('Visible', 'off');
app.ODEeducationalUIFigure.Position = [700 400 789 278];
app.ODEeducationalUIFigure.Name = 'ODE educational';
% Create UIAxes
app.UIAxes = uiaxes(app.ODEeducationalUIFigure);
zlabel(app.UIAxes, 'Z')
app.UIAxes.FontSize = 14;
app.UIAxes.Position = [406 10 371 259];
% Create Panel
app.Panel = uipanel(app.ODEeducationalUIFigure);
app.Panel.BorderType = 'none';
app.Panel.Position = [2 1 397 278];
% Create CalculateButton
app.CalculateButton = uibutton(app.Panel, 'push');
app.CalculateButton.ButtonPushedFcn = createCallbackFcn(app, @CalculateButtonPushed, true);
app.CalculateButton.Position = [150 186 108 22];
app.CalculateButton.Text = 'Calculate';
% Create ClearButton
app.ClearButton = uibutton(app.Panel, 'push');
app.ClearButton.ButtonPushedFcn = createCallbackFcn(app, @ClearButtonPushed, true);
app.ClearButton.Position = [268 186 121 22];
app.ClearButton.Text = 'Clear';
% Create PlotButton
app.PlotButton = uibutton(app.Panel, 'push');
app.PlotButton.ButtonPushedFcn = createCallbackFcn(app, @PlotButtonPushed, true);
app.PlotButton.Position = [268 10 121 22];
app.PlotButton.Text = 'Plot';
% Create SolutionTextAreaLabel
app.SolutionTextAreaLabel = uilabel(app.Panel);
app.SolutionTextAreaLabel.HorizontalAlignment = 'right';
app.SolutionTextAreaLabel.Position = [92 77 49 67];
app.SolutionTextAreaLabel.Text = 'Solution';
% Create solution
app.solution = uitextarea(app.Panel);
app.solution.Position = [150 43 239 135];
% Create RangeEditFieldLabel
app.RangeEditFieldLabel = uilabel(app.Panel);
app.RangeEditFieldLabel.HorizontalAlignment = 'right';
app.RangeEditFieldLabel.Position = [92 10 49 22];
app.RangeEditFieldLabel.Text = 'Range';
% Create range
app.range = uieditfield(app.Panel, 'text');
app.range.Position = [150 10 108 22];
app.range.Value = '[0,50]';
% Create InitialconditionsEditFieldLabel
app.InitialconditionsEditFieldLabel = uilabel(app.Panel);
app.InitialconditionsEditFieldLabel.HorizontalAlignment = 'right';
app.InitialconditionsEditFieldLabel.Position = [31 216 110 22];
app.InitialconditionsEditFieldLabel.Text = 'Initial conditions';
% Create initial_conditions
app.initial_conditions = uieditfield(app.Panel, 'text');
app.initial_conditions.Position = [150 216 239 22];
app.initial_conditions.Value = 'y(0)=0,Dy(0)=0';
% Create OrdDifferentialequationLabel
app.OrdDifferentialequationLabel = uilabel(app.Panel);
app.OrdDifferentialequationLabel.HorizontalAlignment = 'right';
app.OrdDifferentialequationLabel.Position = [-36 247 177 22];
app.OrdDifferentialequationLabel.Text = 'Ord. Differential equation';
% Create ode
app.ode = uieditfield(app.Panel, 'text');
app.ode.Position = [150 247 239 22];
app.ode.Value = 'D2y+3*Dy+2*y=sin(t)';
% Show the figure after all components are created
app.ODEeducationalUIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = example_ODE_app
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.ODEeducationalUIFigure)
% Execute the startup function
runStartupFcn(app, @startupFcn)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.ODEeducationalUIFigure)
end
end
end
ODE educational app
set(gca, 'LineWidth', 1.2, 'FontSize', 14, 'BoxStyle', 'full')
Do not use gca with App Designer. gca applies only to traditional figures.
HI @Alberto Cuadra Lara IT SHOWS SOME ERROR .SO I CANT RUN THE EXE. FILE.ERRORS ARE ---
Error using symengine
Unexpected 'identifier'.
Error in mupadengine/evalin_internal (line 113)
res = mupadmex(statement,output_type{:});
Error in dsolve>mupadDsolve (line 327)
sys = [sys_sym reshape(evalin_internal(symengine, sys_str), 1, [])];
Error in dsolve (line 183)
sol = mupadDsolve(args, options
IN COMMAND WINDOW--
--------------------
appdesigner
Warning: Support of character vectors and strings will be removed in a future release. Use sym objects to define differential equations
instead.
Error using symengine
Unexpected 'identifier'.
Error in mupadengine/evalin_internal (line 113)
res = mupadmex(statement,output_type{:});
Error in dsolve>mupadDsolve (line 327)
sys = [sys_sym reshape(evalin_internal(symengine, sys_str), 1, [])];
Error in dsolve (line 183)
sol = mupadDsolve(args, options);
Error in ODE_Solver/CalculateButtonPushed (line 38)
app.ode_solution = dsolve(eqn, init);
Error using matlab.ui.control.internal.controller.ComponentController/executeUserCallback (line 335)
Error while evaluating Button PrivateButtonPushedFcn.
>>
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.

Accedi per commentare.

Categorie

Scopri di più su Develop Apps Programmatically in Centro assistenza e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by