implementation loop load into app designer

3 visualizzazioni (ultimi 30 giorni)
martin Kostovcik
martin Kostovcik il 6 Mag 2022
Risposto: martin Kostovcik il 7 Mag 2022
Hi, girls and guys,
I have a question how can I implement this type of code into appdesigner can any one help me? There is loop where I need o load variables. Appdesigner is quite new for me and I need to see how its works.
I need to print that "f" on botton into table samehow
Thank you in advance for your help. Im just a student I want to learn.
this is my code :
classdef app3 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
UITable matlab.ui.control.Table
vypocetButton matlab.ui.control.StateButton
ZadajpociatocnehodnotyvektoraxEditField matlab.ui.control.NumericEditField
ZadajpociatocnehodnotyvektoraxEditFieldLabel matlab.ui.control.Label
ZadajnelinearnerovniceEditField matlab.ui.control.EditField
ZadajnelinearnerovniceEditFieldLabel matlab.ui.control.Label
ZadajpresnostrieseniaEditField matlab.ui.control.NumericEditField
ZadajpresnostrieseniaEditFieldLabel matlab.ui.control.Label
ZadajpocetnelinearnychrovnicEditField matlab.ui.control.NumericEditField
ZadajpocetnelinearnychrovnicEditFieldLabel matlab.ui.control.Label
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: vypocetButton
function vypocetButtonValueChanged(app, event)
function in=data()
in.n = app.ZadajpocetnelinearnychrovnicEditField.Value;
in.eps = app.ZadajpresnostrieseniaEditField.Value;
in.f(i) = app.ZadajnelinearnerovniceEditField.Value;
in.x(i) = app.ZadajpociatocnehodnotyvektoraxEditField.Value;
end
function [x1]=newton(in)
X=symvar(in.f);
% Finding symbolic variables in the system f
n=length(X); % Finding the number of variables in the vector x
x1=zeros(1,n); % Defining auxiliary vector x (k + 1)
x0=in.x; %vektor x(k)
J=jacobian(in.f,X); % Determination of Jacobi matrix components J (x)
podmienka=true; % Set that -f (x (k))) / J (x (k))> e
while podmienka
f=double(subs(in.f,X,x0)); % f(x(k))
J=double(subs(J,X,x0)); % J(x(k))
%calculate of f(x(k)))/J(x(k))
prirastok=(inv(J)*f')'; % The result is the increment vector dx1, dx2, ..., dxn
x1=x0-prirastok; % Calculation of vector values x (k + 1) according to (3.13)
eps_p=max(abs(-prirastok));
x0=x1; % Assignment x (k) = x (k + 1)
podmienka=eps_p>in.eps; %Test, if -f(x(k)))/J(x(k))>e
end
end
in = data(); % Loading input data
for k=1:length(in) %print output
in(k)
end
in.x_1=newton(in); %call Newton
X=symvar(in.f); % Finding symbolic variables in the system f
x=solve(in.f, 'Real',true); % Matlab function call solve
p=length(x.(['x',num2str(1)])) %Determine the number of stationary points for x_1
for i=1:in.n
for h=1:p
x_out(i,h)=x.(['x',num2str(i)])(h);
end
end
for h=1:p % Substitution of stationary points x_h into the system f
for i=1:in.n
% Acquisition of stationary point values x_h
in.x(i)=double(x_out(i,h));
end
for i=1:in.n
fprintf('f(%d)=%g\n', i,double(subs(in.f(i),X,in.x))); % Only this is important to print somehow
end
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 = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create ZadajpocetnelinearnychrovnicEditFieldLabel
app.ZadajpocetnelinearnychrovnicEditFieldLabel = uilabel(app.UIFigure);
app.ZadajpocetnelinearnychrovnicEditFieldLabel.HorizontalAlignment = 'right';
app.ZadajpocetnelinearnychrovnicEditFieldLabel.Position = [34 406 178 22];
app.ZadajpocetnelinearnychrovnicEditFieldLabel.Text = 'Zadaj pocet nelinearnych rovnic:';
% Create ZadajpocetnelinearnychrovnicEditField
app.ZadajpocetnelinearnychrovnicEditField = uieditfield(app.UIFigure, 'numeric');
app.ZadajpocetnelinearnychrovnicEditField.Position = [227 406 100 22];
% Create ZadajpresnostrieseniaEditFieldLabel
app.ZadajpresnostrieseniaEditFieldLabel = uilabel(app.UIFigure);
app.ZadajpresnostrieseniaEditFieldLabel.HorizontalAlignment = 'right';
app.ZadajpresnostrieseniaEditFieldLabel.Position = [79 363 133 22];
app.ZadajpresnostrieseniaEditFieldLabel.Text = 'Zadaj presnost riesenia:';
% Create ZadajpresnostrieseniaEditField
app.ZadajpresnostrieseniaEditField = uieditfield(app.UIFigure, 'numeric');
app.ZadajpresnostrieseniaEditField.Position = [227 363 100 22];
% Create ZadajnelinearnerovniceEditFieldLabel
app.ZadajnelinearnerovniceEditFieldLabel = uilabel(app.UIFigure);
app.ZadajnelinearnerovniceEditFieldLabel.HorizontalAlignment = 'right';
app.ZadajnelinearnerovniceEditFieldLabel.Position = [72 315 140 22];
app.ZadajnelinearnerovniceEditFieldLabel.Text = 'Zadaj nelinearne rovnice:';
% Create ZadajnelinearnerovniceEditField
app.ZadajnelinearnerovniceEditField = uieditfield(app.UIFigure, 'text');
app.ZadajnelinearnerovniceEditField.Position = [227 315 100 22];
% Create ZadajpociatocnehodnotyvektoraxEditFieldLabel
app.ZadajpociatocnehodnotyvektoraxEditFieldLabel = uilabel(app.UIFigure);
app.ZadajpociatocnehodnotyvektoraxEditFieldLabel.HorizontalAlignment = 'right';
app.ZadajpociatocnehodnotyvektoraxEditFieldLabel.Position = [13 263 199 22];
app.ZadajpociatocnehodnotyvektoraxEditFieldLabel.Text = 'Zadaj pociatocne hodnoty vektora x:';
% Create ZadajpociatocnehodnotyvektoraxEditField
app.ZadajpociatocnehodnotyvektoraxEditField = uieditfield(app.UIFigure, 'numeric');
app.ZadajpociatocnehodnotyvektoraxEditField.Position = [227 263 100 22];
% Create vypocetButton
app.vypocetButton = uibutton(app.UIFigure, 'state');
app.vypocetButton.ValueChangedFcn = createCallbackFcn(app, @vypocetButtonValueChanged, true);
app.vypocetButton.Text = 'vypocet';
app.vypocetButton.Position = [227 200 100 23];
% Create UITable
app.UITable = uitable(app.UIFigure);
app.UITable.ColumnName = {'Column 1'; 'Column 2'; 'Column 3'; 'Column 4'};
app.UITable.RowName = {};
app.UITable.Position = [126 1 302 185];
% 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 = app3
% 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
and this is a code what I try to implement
Start.m
in = data(); %Načítanie vstupných údajov
for k=1:length(in) %Výpis vstupných údajov na obrazovku
in(k)
end
in.x_1=newton(in); %Volanie Newtonovej metódy
disp(['Newton:']);
disp(['Výsledný vektor x:']);
for i=1:in.n %Vypis konecnych hodnot vektora x
fprintf('x(%d)=%g\n' , i,in.x_1(i));
end
X=symvar(in.f); %Najdenie symbolickych premennych v sustave f
disp(['1. Test - Dosadenie vektora x do funkcií:']);
for i=1:in.n %Vypis konecnych hodnot vektora x
fprintf('f(%d)=%g\n', i,double(subs(in.f(i),X,in.x_1))); %Dosadenie vektora x do funkcií
end
x=solve(in.f, 'Real',true); %Volanie Matlabovskej funkcie solve
p=length(x.(['x',num2str(1)])) %Urcenie poctup stacionarnzch bodov pre x_1
for i=1:in.n
for h=1:p
x_out(i,h)=x.(['x',num2str(i)])(h);
end
end
disp(['Solve:']);
disp(['Výsledný vektor resp. vektory x:']); %Vypis stacionarnych bodov x_h
for h=1:p
fprintf('Vektor - %d\n' , h);
for i=1:in.n
fprintf('x(%d)=%g\n', i,double(x_out(i,h)));
end
end
disp(['2. Test - Dosadenie vektora x do funkcií:']);
for h=1:p %Dosadenie stacionarnych bodov x_h do sustavy f
fprintf('Vektor - %d\n' , h);
for i=1:in.n %Ziskanie hodnot stacionarneho bodu x_h
in.x(i)=double(x_out(i,h));
end
for i=1:in.n
fprintf('f(%d)=%g\n', i,double(subs(in.f(i),X,in.x)));
end
end
data.m
function in=data()
%Testovaci priklad
disp(['Zadaj pocet nelinearnych rovnic:']);
in.n=input('n='); %Nacitanie poctu nelinearnych funkcii
disp(['Zadaj presnost riesenia:']);
in.eps=input('presnost='); %Nacitanie presnosti e
disp(['Zadaj nelinearne rovnice:']);
for i=1:in.n
fprintf('f(%d)' , i);
in.f(i)=str2sym(input('=')); %Nacitanie nelinearnych funkcii, vytvorenie f
end
disp(['Zadaj pociatocne hodnoty vektora x:']);
for i=1:in.n
fprintf('x(%d)' , i);
in.x(i)=input('='); %nacitanie pociatocneho vektora x(k)
end
end
and the newton.m this is just a calculations
function [x1]=newton(in)
X=symvar(in.f); %Nájdenie symbolických premenných v sústave f
n=length(X); %Zistenie počtu premenných vo vektore x
x1=zeros(1,n); %Definovanie pomocného vektora x(k+1)
x0=in.x; %vektor x(k)
J=jacobian(in.f,X); %Určenie zložiek Jacobiovej matice J(x)
podmienka=true %Nastavenie, že -f(x(k)))/J(x(k))>e
while podmienka
f=double(subs(in.f,X,x0)); %urcenie f(x(k))
J=double(subs(J,X,x0)); %urcenie J(x(k))
%Vypocet f(x(k)))/J(x(k))
prirastok=(inv(J)*f')'; %Vysledok je vektor prirastkov dx1, dx2, ..., dxn
x1=x0-prirastok; %Vypocet hodnot vektora x(k+1) podla (3.13)
eps_p=max(abs(-prirastok));
x0=x1; %Priradenie x(k)=x(k+1)
podmienka=eps_p>in.eps; %Test, ci -f(x(k)))/J(x(k))>e
end
end
  3 Commenti
martin Kostovcik
martin Kostovcik il 6 Mag 2022
execuse me,
I mean this as "f"
for i=1:in.n
fprintf('f(%d)=%g\n', i,double(subs(in.f(i),X,in.x))); % Only this is important to print somehow
end
yes, I totally understand, but this is a code from my teacher and I have to use this code as he wrote it
Monica Roberts
Monica Roberts il 6 Mag 2022
It really depends on when you want your code to run. You probably want to add callbacks to start running the code. You can add callbacks by adding a component in the "Design View" and then add a callback: https://www.mathworks.com/help/matlab/creating_guis/code-and-call-app-functions-in-app-designer.html
To run your functions within the app, you can create private or public functions: https://www.mathworks.com/help/matlab/creating_guis/code-and-call-app-functions-in-app-designer.html
Your app can display your results at the MATLAB Command Window like normal using disp or fprintf. But you could also use a text area to show the results. Or you could try to use a table. And to input data from the user, instead of the "input" function, you could try an edit field: https://www.mathworks.com/products/matlab/app-designer/component-gallery.html
The documentation is pretty vast. You may want to try following some tutorials or examples: https://www.mathworks.com/help/matlab/creating_guis/create-a-simple-app-or-gui-using-app-designer.html

Accedi per commentare.

Risposte (2)

Willingo
Willingo il 7 Mag 2022
This is incredibly hard to read, and posting the app designer code doesn't help nearly as much as uploading the .mlapp file would.
I don't know if or how fprintf can be used with tables, and you could skip the for loop with compose somehow, but from what I can tell you are creating text of the form "f(x) = number". If you build a string array row-wise for each index, then you can use that to make a table and then store that into uitable.Data.
Take careful note of how we use strings, double quotes " ", and not char vectors, as strings are more suited to arrays. I also used sprintf, since we aren't writing to text or to the command window but to an array.
This replicates the app designer in scripts.
close all force %closes previous uifigure
% build function f(x) = x^2 + 5
for i = 1:10
num1 = i;
num2 = i.^2 + 5;
ourStringArray(i,1) = sprintf("f(%d)=%g", num1,num2);
end
% Create UIFigure and UITable. App designer already does this for you
uif = uifigure; %uifigure handle
uit = uitable(uif);
%if you do not assign col name it inherits "ourStringArray". Name "test" to
%whatever you want
ourTable = table(ourStringArray,'VariableNames', "test");
%Store the table into the UITable. You would have app.UITable.Data (UITable might have a differentname. Check on design view)
uit.Data = ourTable;

martin Kostovcik
martin Kostovcik il 7 Mag 2022
Yeah, it really bad, but I need to know how can I write this type of code into appdesigner :( its is quite new for me and I don´t understand how can I do that

Categorie

Scopri di più su Develop Apps Using App Designer in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by