Error using odearguments and Error in ode45

1 view (last 30 days)
hi i try to do some simulation and i cant understand what the problem is when i try to combine it in app designer
the for the helpers
sorry for the long locking code
classdef app1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
ShowGraphButton matlab.ui.control.Button
TheconstantthatlimitstherateofpredationEditField matlab.ui.control.NumericEditField
TheconstantthatlimitsthepopulationofthepreyspeciesEditField matlab.ui.control.NumericEditField
EditField7Label matlab.ui.control.Label
TheconstantthatlimitstherateofpredationEditFieldLabel matlab.ui.control.Label
dEditField matlab.ui.control.NumericEditField
TheconstantrateofdisappearanceofthecarnivorousspeciesLabel matlab.ui.control.Label
ThegrowthrateofthecarnivorousspeciespopulationEditField matlab.ui.control.NumericEditField
ThegrowthrateofthecarnivorousspeciespopulationEditFieldLabel matlab.ui.control.Label
TherateofdisappearanceofthepreyspeciespopulationEditField matlab.ui.control.NumericEditField
TherateofdisappearanceofthepreyspeciespopulationEditFieldLabel matlab.ui.control.Label
ThegrowthrateofthepreyspeciespopulationEditField matlab.ui.control.NumericEditField
ThegrowthrateofthepreyspeciespopulationEditFieldLabel matlab.ui.control.Label
ThePredatorspeciespopulationEditField matlab.ui.control.NumericEditField
ThePredatorspeciespopulationEditFieldLabel matlab.ui.control.Label
ThePopulationofthepreyspeciesEditField matlab.ui.control.NumericEditField
ThePopulationofthepreyspeciesEditFieldLabel matlab.ui.control.Label
UIAxes matlab.ui.control.UIAxes
end
properties (Access = public)
y1;y2;a;bF;cR;d;k;s;ck=0;f=0; % Description
t=0:100;
Y;
end
properties (Access = private)
end
methods (Access = private)
% y1;y2;a;bF;cR;d;k;s;t;Y;ck;f;
function dFRdt = evolution(t,Y)
dFRdt(1,:)=app.a*(1-(Y(1)/app.k))*Y(1)-app.bF*(Y(1)*Y(2)/(1+Y(1)/app.s)); %f(1,1) vector for input values in first row and first col.
dFRdt(2,:)=app.cR*(Y(1)*Y(2)/(1+(Y(1)/app.s)))-app.d*Y(2); %f(2,1) vector for input values in second row and first col.
end
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function:
% ThegrowthrateofthecarnivorousspeciespopulationEditField
function ThegrowthrateofthecarnivorousspeciespopulationEditFieldValueChanged(app, event)
app.cR = app.ThegrowthrateofthecarnivorousspeciespopulationEditField.Value;
end
% Value changed function:
% ThePopulationofthepreyspeciesEditField
function ThePopulationofthepreyspeciesEditFieldValueChanged(app, event)
app.y1 = app.ThePopulationofthepreyspeciesEditField.Value;
end
% Value changed function:
% ThePredatorspeciespopulationEditField
function ThePredatorspeciespopulationEditFieldValueChanged(app, event)
app.y2 = app.ThePredatorspeciespopulationEditField.Value;
end
% Value changed function:
% ThegrowthrateofthepreyspeciespopulationEditField
function ThegrowthrateofthepreyspeciespopulationEditFieldValueChanged(app, event)
app.a = app.ThegrowthrateofthepreyspeciespopulationEditField.Value;
end
% Value changed function:
% TherateofdisappearanceofthepreyspeciespopulationEditField
function TherateofdisappearanceofthepreyspeciespopulationEditFieldValueChanged(app, event)
app.bF = app.TherateofdisappearanceofthepreyspeciespopulationEditField.Value;
end
% Value changed function: dEditField
function dEditFieldValueChanged(app, event)
app.d = app.dEditField.Value;
end
% Value changed function:
% TheconstantthatlimitsthepopulationofthepreyspeciesEditField
function TheconstantthatlimitsthepopulationofthepreyspeciesEditFieldValueChanged(app, event)
app.k = app.TheconstantthatlimitsthepopulationofthepreyspeciesEditField.Value;
end
% Value changed function:
% TheconstantthatlimitstherateofpredationEditField
function TheconstantthatlimitstherateofpredationEditFieldValueChanged(app, event)
app.s = app.TheconstantthatlimitstherateofpredationEditField.Value;
end
% Button pushed function: ShowGraphButton
function ShowGraphButtonPushed(app, event)
app.t=0:100;
app.y1;app.y2;
[app.ck,app.f] = ode45(@(t,Y) app.evolution(t,Y) , [0,100] ,[app.y2 app.y1]);
plot(app.UIAxes,app.ck,app.f(:,1),'r');
grid on; hold on;
plot(app.UIAxes,app.ck,app.f(:,2),'b');
end
% Button down function: UIAxes
function UIAxesButtonDown(app, event)
end
% Callback function
function SliderValueChanged(app, event)
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 1165 722];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Predator Prey Quantity over time')
xlabel(app.UIAxes, 'time')
ylabel(app.UIAxes, 'Quantity')
zlabel(app.UIAxes, 'Z')
app.UIAxes.FontWeight = 'bold';
app.UIAxes.FontSize = 20;
app.UIAxes.ButtonDownFcn = createCallbackFcn(app, @UIAxesButtonDown, true);
app.UIAxes.Position = [388 222 735 456];
% Create ThePopulationofthepreyspeciesEditFieldLabel
app.ThePopulationofthepreyspeciesEditFieldLabel = uilabel(app.UIFigure);
app.ThePopulationofthepreyspeciesEditFieldLabel.HorizontalAlignment = 'right';
app.ThePopulationofthepreyspeciesEditFieldLabel.Position = [36 668 191 22];
app.ThePopulationofthepreyspeciesEditFieldLabel.Text = 'The Population of the prey species';
% Create ThePopulationofthepreyspeciesEditField
app.ThePopulationofthepreyspeciesEditField = uieditfield(app.UIFigure, 'numeric');
app.ThePopulationofthepreyspeciesEditField.ValueChangedFcn = createCallbackFcn(app, @ThePopulationofthepreyspeciesEditFieldValueChanged, true);
app.ThePopulationofthepreyspeciesEditField.Position = [58 642 155 22];
app.ThePopulationofthepreyspeciesEditField.Value = 600;
% Create ThePredatorspeciespopulationEditFieldLabel
app.ThePredatorspeciespopulationEditFieldLabel = uilabel(app.UIFigure);
app.ThePredatorspeciespopulationEditFieldLabel.HorizontalAlignment = 'right';
app.ThePredatorspeciespopulationEditFieldLabel.Position = [28 601 178 22];
app.ThePredatorspeciespopulationEditFieldLabel.Text = 'The Predator species population';
% Create ThePredatorspeciespopulationEditField
app.ThePredatorspeciespopulationEditField = uieditfield(app.UIFigure, 'numeric');
app.ThePredatorspeciespopulationEditField.ValueChangedFcn = createCallbackFcn(app, @ThePredatorspeciespopulationEditFieldValueChanged, true);
app.ThePredatorspeciespopulationEditField.Position = [58 581 155 20];
app.ThePredatorspeciespopulationEditField.Value = 400;
% Create ThegrowthrateofthepreyspeciespopulationEditFieldLabel
app.ThegrowthrateofthepreyspeciespopulationEditFieldLabel = uilabel(app.UIFigure);
app.ThegrowthrateofthepreyspeciespopulationEditFieldLabel.HorizontalAlignment = 'right';
app.ThegrowthrateofthepreyspeciespopulationEditFieldLabel.Position = [15 535 256 22];
app.ThegrowthrateofthepreyspeciespopulationEditFieldLabel.Text = 'The growth rate of the prey species population';
% Create ThegrowthrateofthepreyspeciespopulationEditField
app.ThegrowthrateofthepreyspeciespopulationEditField = uieditfield(app.UIFigure, 'numeric');
app.ThegrowthrateofthepreyspeciespopulationEditField.ValueChangedFcn = createCallbackFcn(app, @ThegrowthrateofthepreyspeciespopulationEditFieldValueChanged, true);
app.ThegrowthrateofthepreyspeciespopulationEditField.Position = [58 505 155 22];
app.ThegrowthrateofthepreyspeciespopulationEditField.Value = 0.4;
% Create TherateofdisappearanceofthepreyspeciespopulationEditFieldLabel
app.TherateofdisappearanceofthepreyspeciespopulationEditFieldLabel = uilabel(app.UIFigure);
app.TherateofdisappearanceofthepreyspeciespopulationEditFieldLabel.HorizontalAlignment = 'right';
app.TherateofdisappearanceofthepreyspeciespopulationEditFieldLabel.Position = [1 465 321 22];
app.TherateofdisappearanceofthepreyspeciespopulationEditFieldLabel.Text = 'The rate of disappearance of the prey species population';
% Create TherateofdisappearanceofthepreyspeciespopulationEditField
app.TherateofdisappearanceofthepreyspeciespopulationEditField = uieditfield(app.UIFigure, 'numeric');
app.TherateofdisappearanceofthepreyspeciespopulationEditField.ValueChangedFcn = createCallbackFcn(app, @TherateofdisappearanceofthepreyspeciespopulationEditFieldValueChanged, true);
app.TherateofdisappearanceofthepreyspeciespopulationEditField.Position = [58 444 155 22];
app.TherateofdisappearanceofthepreyspeciespopulationEditField.Value = 0.004;
% Create ThegrowthrateofthecarnivorousspeciespopulationEditFieldLabel
app.ThegrowthrateofthecarnivorousspeciespopulationEditFieldLabel = uilabel(app.UIFigure);
app.ThegrowthrateofthecarnivorousspeciespopulationEditFieldLabel.HorizontalAlignment = 'right';
app.ThegrowthrateofthecarnivorousspeciespopulationEditFieldLabel.Position = [14 406 295 25];
app.ThegrowthrateofthecarnivorousspeciespopulationEditFieldLabel.Text = 'The growth rate of the carnivorous species population';
% Create ThegrowthrateofthecarnivorousspeciespopulationEditField
app.ThegrowthrateofthecarnivorousspeciespopulationEditField = uieditfield(app.UIFigure, 'numeric');
app.ThegrowthrateofthecarnivorousspeciespopulationEditField.ValueChangedFcn = createCallbackFcn(app, @ThegrowthrateofthecarnivorousspeciespopulationEditFieldValueChanged, true);
app.ThegrowthrateofthecarnivorousspeciespopulationEditField.Position = [58 383 155 22];
app.ThegrowthrateofthecarnivorousspeciespopulationEditField.Value = 0.004;
% Create TheconstantrateofdisappearanceofthecarnivorousspeciesLabel
app.TheconstantrateofdisappearanceofthecarnivorousspeciesLabel = uilabel(app.UIFigure);
app.TheconstantrateofdisappearanceofthecarnivorousspeciesLabel.HorizontalAlignment = 'right';
app.TheconstantrateofdisappearanceofthecarnivorousspeciesLabel.Position = [8 338 342 22];
app.TheconstantrateofdisappearanceofthecarnivorousspeciesLabel.Text = 'The constant rate of disappearance of the carnivorous species';
% Create dEditField
app.dEditField = uieditfield(app.UIFigure, 'numeric');
app.dEditField.ValueChangedFcn = createCallbackFcn(app, @dEditFieldValueChanged, true);
app.dEditField.Position = [58 312 155 22];
app.dEditField.Value = 0.9;
% Create TheconstantthatlimitstherateofpredationEditFieldLabel
app.TheconstantthatlimitstherateofpredationEditFieldLabel = uilabel(app.UIFigure);
app.TheconstantthatlimitstherateofpredationEditFieldLabel.HorizontalAlignment = 'right';
app.TheconstantthatlimitstherateofpredationEditFieldLabel.Position = [17 202 242 22];
app.TheconstantthatlimitstherateofpredationEditFieldLabel.Text = 'The constant that limits the rate of predation';
% Create EditField7Label
app.EditField7Label = uilabel(app.UIFigure);
app.EditField7Label.HorizontalAlignment = 'right';
app.EditField7Label.Position = [15 277 316 22];
app.EditField7Label.Text = 'The constant that limits the population of the prey species';
% Create TheconstantthatlimitsthepopulationofthepreyspeciesEditField
app.TheconstantthatlimitsthepopulationofthepreyspeciesEditField = uieditfield(app.UIFigure, 'numeric');
app.TheconstantthatlimitsthepopulationofthepreyspeciesEditField.ValueChangedFcn = createCallbackFcn(app, @TheconstantthatlimitsthepopulationofthepreyspeciesEditFieldValueChanged, true);
app.TheconstantthatlimitsthepopulationofthepreyspeciesEditField.Position = [58 256 155 22];
app.TheconstantthatlimitsthepopulationofthepreyspeciesEditField.Value = 800;
% Create TheconstantthatlimitstherateofpredationEditField
app.TheconstantthatlimitstherateofpredationEditField = uieditfield(app.UIFigure, 'numeric');
app.TheconstantthatlimitstherateofpredationEditField.ValueChangedFcn = createCallbackFcn(app, @TheconstantthatlimitstherateofpredationEditFieldValueChanged, true);
app.TheconstantthatlimitstherateofpredationEditField.Position = [58 181 155 22];
app.TheconstantthatlimitstherateofpredationEditField.Value = 600;
% Create ShowGraphButton
app.ShowGraphButton = uibutton(app.UIFigure, 'push');
app.ShowGraphButton.ButtonPushedFcn = createCallbackFcn(app, @ShowGraphButtonPushed, true);
app.ShowGraphButton.FontSize = 20;
app.ShowGraphButton.FontWeight = 'bold';
app.ShowGraphButton.Position = [36 61 252 76];
app.ShowGraphButton.Text = 'Show Graph';
% 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 = app1
% 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
  1 Comment
Cris LaPierre
Cris LaPierre on 18 Jan 2022
It is much more helpful to attach your mlapp file so that we can run it. You can do that using the paperclip icon.

Sign in to comment.

Answers (1)

Cris LaPierre
Cris LaPierre on 18 Jan 2022
As an initial guess, I wonder if app.y2 and app.y1 have not yet been assigned values. Currently, most of your variables will only be assigned a value if their callback function is executed. So if you try to simulate using the default values, you will get an error.
My suggestion would be to add a startup function that assigns all these variables their default value w/o each callback having to be run.
function startupFcn(app)
app.cR = app.ThegrowthrateofthecarnivorousspeciespopulationEditField.Value;
app.y1 = app.ThePopulationofthepreyspeciesEditField.Value;
app.y2 = app.ThePredatorspeciespopulationEditField.Value;
app.a = app.ThegrowthrateofthepreyspeciespopulationEditField.Value;
app.bF = app.TherateofdisappearanceofthepreyspeciespopulationEditField.Value;
app.d = app.dEditField.Value;
app.k = app.TheconstantthatlimitsthepopulationofthepreyspeciesEditField.Value;
app.s = app.TheconstantthatlimitstherateofpredationEditField.Value;
end
Another option is to get rid of all the callbacks, and just do the same assignment at the top of your ShowGraphButtonPushed callback function. It will ensure the simulation always runs using the current values.

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!