Sliders Not Appearing on Plot
Ältere Kommentare anzeigen
I am working on a script for the Friedmann equation and I am trying to code a group of sliders to appear on the plot. I have a working function for the Friedmann equation plot and a separate working function for the sliders, but I am having trouble getting the sliders to appear on the figure with this resulting plot. Below is the code I am working with. I am not sure if I am missing any coding or I have some kind of error within the script that is preventing them from appearing, like a missing variable or possibly something I simply wrote incorrectly.
function friedmann
% Define figure, normalized position can be adjusted to fit individual
% monitors.
fig = figure('units','normalized',...
'Position', [0.515 0.025 0.415 0.87],... %%%%
'name','Friedmann');
% Define axes so that room is available in figure window for sliders
axis = axes('unit','normalized',...
'position',[0.1 0.5 0.8 0.45]);
%%%% Actual Hubble value (m.Gyr^-1)
H0=67400/(3.09*10^(22))*(3600*24*365*10^(9));
%%%% Starting and ending time
t_begin(1) = 0.38;
t_final(1) = -15;
t_begin(2) = 0.38;
t_final(2) = 30;
%%%% Initial Omega parameters
Omega1_m = 0.05;
Omega1_r = 10^-4;
Omega1_l = 0.95;
Omega1_k = 1-Omega1_m-Omega1_r-Omega1_l;
%%%% Initial scale factor and derivated scale factor
a1_0=1;
a1_prim_0=H0*(Omega1_m/a1_0+Omega1_r/(a1_0^2)+Omega1_k+Omega1_l*(a1_0^2))^(1/2);
init1=[ a1_0 ; a1_prim_0 ];
set(groot,'defaultLineLineWidth',1.0)
%%%% Solving in two parts : backwards and forwards
for i=1:2
%%%% options for solver
options = odeset('Events',@events,'RelTol',10^(-10),'AbsTol',10^(-10));
%%%% Differential systems solving
[t1,y1]=ode45(@(t1,y1) syseq(t1,y1,Omega1_m,Omega1_r,Omega1_l,Omega1_k),[t_begin(i) t_final(i)],init1,options);
figure(1);
hold on;
%%%% Plot solutions
plot(t1,y1(:,1),'black');
box on;
grid on;
xlabel('Age');
ylabel('Size');
ylim([ 0 12]);
%%%% Set label for each curve
posX = -13;
posX1 = posX;
posY1 = 3.6;
title('Numerical Solution of the Friedmann Equation');
end
end
%%%% Differential system function
function dydt = syseq(t,y,Omega_m,Omega_r,Omega_l,Omega_k)
%%%% Actual Hubble value
H0=67400/(3.09*10^(22))*(3600*24*365*10^(9));
dydt=[ y(2) ;
(-(H0^(2)/2)*(Omega_m/y(1)^(3)+Omega_r/y(1)^(4)-2*Omega_l))*Omega_m*(1/(H0^(2))*y(2)^(2)-...
Omega_r/y(1)^(2)-Omega_l*y(1)^(2)-Omega_k)^(-1);];
end
%%%% Integration stopping function
function [value,isterminal,direction] = events(t,y)
% Locate the time when height passes through zero in a
% decreasing direction and stop integration.
value = [y(1),4-y(1)]; % Detect height = 0
isterminal = [1,1]; % Stop the integration
direction = [-1,1]; % Negative and positive directions
end
function slider
% Omega variables
Omega_m = 0.05;
Omega_l = 0.95;
t_begin(1) = 0.38;
% Interval to end the function.
S.inter = 0:0.05:2;
% First Slider:
Omega_l.slider = uicontrol('style','slider',...
'unit','normalized',...
'position',[0.2 0.1 0.7 0.01],...
'min',0,'max',1,'value', Omega_l,...
'sliderstep',[0.05 0.05],...
'callback', {@SliderCB, 'Omega1_l'});
% Add a text uicontrol to label the slider.
txtl = uicontrol('Style','text',...
'unit','normalized',...
'position',[0.2 0.11 0.7 0.02],...
'String','Vacuum Density');
% Second Slider:
t_begin.slider = uicontrol('style','slide',...
'unit','normalized',...
'position',[0.2 0.15 0.7 0.01],...
'min',-1,'max',1,'value', t_begin(1),...
'sliderstep',[0.01 0.01],...
'callback', {@SliderCB, 't_begin'});
% Add a text uicontrol to label the slider.
txtb = uicontrol('Style','text',...
'unit','normalized',...
'position',[0.2 0.16 0.7 0.02],...
'String','Present Time, t_begin');
% Third Slider:
Omega_m.Slider = uicontrol('style','slide',...
'unit','normalized',...
'position',[0.2 0.2 0.7 0.01],...
'min',0,'max',3,'value', Omega_m,...
'sliderstep',[0.01 0.01],...
'callback', {@SliderCB, 'Omega1_M'});
% Add a text uicontrol to label the slider.
txtm = uicontrol('Style','text',...
'unit','normalized',...
'position',[0.2 0.21 0.7 0.02],...
'String','Matter Density, Omega_M');
guidata(fig, S); % Store S structure in the figure
end
% Callback for all sliders defined above.
function SliderCB(aSlider, EventData, Param)
S = guidata(aSlider); % Get S structure from the figure
S.(Param) = get(aSlider, 'Value'); % Any of the 'a', 'b', etc. defined
update(S); % Update the plot values
guidata(aSlider, S); % Store modified S in figure
end
% Vector with new plot.
function update(S)
y = a1_prim_0(S.inter,[Omega_l,t_begin(1),Omega_m]); % function
set(plot(t1,y1(:,1),'black'), 'YData', y); % Replace old plot with new plotting values
end
Below is the figure that I get from running the file. The figure leaves open a space below the plot for the three sliders to fit (for the variables OmegaL, OmegaM, and tbegin), but the sliders are not appearing with the figure as I need them. What should I do or what should I add to get the sliders to appear with the plot on the figure?
Any and all help is greatly appreciated. Thank you very much in advance.

5 Kommentare
Jordan Watts
am 18 Jul. 2022
Jan
am 18 Jul. 2022
Your slider function does not have any inputs. In addition the variables Omega_m, Omega_l and t_begin are scalar constants. What is the purpose of using them a structs with the fields "aSlider", "bSlider" and "cSlider"?
Jonas
am 18 Jul. 2022
without knowing what are you exactly you want to do with in your gui, here is a cleaned up version which has a single update function, which pulss its parameters directly from the sliders. Since I never used ode45 or similar, you have to adjust it to make it working with sence
function friedmann
close all
% Define figure, normalized position can be adjusted to fit individual
% monitors.
fig = figure('units','normalized',...
'Position', [0.515 0.025 0.415 0.87],... %%%%
'name','Friedmann');
% Define axes so that room is available in figure window for sliders
ax = axes('unit','normalized',...
'position',[0.1 0.5 0.8 0.45]);
%%%% Actual Hubble value (m.Gyr^-1)
H0=67400/(3.09*10^(22))*(3600*24*365*10^(9));
%%%% Starting and ending time
t_begin(1) = 0.38;
t_final(1) = -15;
t_begin(2) = 0.38;
t_final(2) = 30;
%%%% Initial Omega parameters
Omega1_m = 0.05;
Omega1_r = 10^-4;
Omega1_l = 0.95;
Omega1_k = 1-Omega1_m-Omega1_r-Omega1_l;
%%%% Initial scale factor and derivated scale factor
a1_0=1;
a1_prim_0=H0*(Omega1_m/a1_0+Omega1_r/(a1_0^2)+Omega1_k+Omega1_l*(a1_0^2))^(1/2);
init1=[ a1_0 ; a1_prim_0 ];
set(groot,'defaultLineLineWidth',1.0)
options = odeset('Events',@events,'RelTol',10^(-10),'AbsTol',10^(-10));
%%%% Solving in two parts : backwards and forwards
for i=1:2
%%%% options for solver
%%%% Differential systems solving
[t1,y1]=ode45(@(t1,y1) syseq(t1,y1,Omega1_m,Omega1_r,Omega1_l,Omega1_k),[t_begin(i) t_final(i)],init1,options);
figure(1);
%%%% Plot solutions
plotLine(i)=plot(t1,y1(:,1),'black');
hold on;
box on;
grid on;
xlabel('Age');
ylabel('Size');
ylim([ 0 12]);
title('Numerical Solution of the Friedmann Equation');
end
interval = 0:0.05:2;
%% create sliders
% First Slider:
Omega_lslider = uicontrol(fig,'style','slider',...
'unit','normalized',...
'position',[0.2 0.1 0.7 0.01],...
'min',0,'max',1,'value', Omega1_l,...
'sliderstep',[0.05 0.05],...
'callback', @update);
% Add a text uicontrol to label the slider.
txtl = uicontrol('Style','text',...
'unit','normalized',...
'position',[0.2 0.11 0.7 0.02],...
'String','Vacuum Density');
% Second Slider:
t_beginslider = uicontrol('style','slide',...
'unit','normalized',...
'position',[0.2 0.15 0.7 0.01],...
'min',-1,'max',1,'value', t_begin(1),...
'sliderstep',[0.01 0.01],...
'callback', @update);
% Add a text uicontrol to label the slider.
txtb = uicontrol('Style','text',...
'unit','normalized',...
'position',[0.2 0.16 0.7 0.02],...
'String','Present Time, t_begin');
% Third Slider:
Omega_mSlider = uicontrol(fig,'style','slide',...
'unit','normalized',...
'position',[0.2 0.2 0.7 0.01],...
'min',0,'max',3,'value', Omega1_m,...
'sliderstep',[0.01 0.01],...
'callback', @update);
% Add a text uicontrol to label the slider.
txtm = uicontrol(fig,'Style','text',...
'unit','normalized',...
'position',[0.2 0.21 0.7 0.02],...
'String','Matter Density, Omega_M');
function update(~,~)
delete(plotLine)
for i=1:2
%%%% options for solver
%%%% Differential systems solving
[t1,y1]=ode45(@(t1,y1) syseq(t1,y1,Omega1_m,Omega1_r,Omega1_l,Omega1_k),[t_begin(i) t_final(i)],init1,options);
%%%% Plot solutions
plotLine(i)=plot(ax,t1,y1(:,1),'black');
end
end
end
%%%% Differential system function
function dydt = syseq(t,y,Omega_m,Omega_r,Omega_l,Omega_k)
%%%% Actual Hubble value
H0=67400/(3.09*10^(22))*(3600*24*365*10^(9));
dydt=[ y(2) ;
(-(H0^(2)/2)*(Omega_m/y(1)^(3)+Omega_r/y(1)^(4)-2*Omega_l))*Omega_m*(1/(H0^(2))*y(2)^(2)-...
Omega_r/y(1)^(2)-Omega_l*y(1)^(2)-Omega_k)^(-1);];
end
%%%% Integration stopping function
function [value,isterminal,direction] = events(t,y)
% Locate the time when height passes through zero in a
% decreasing direction and stop integration.
value = [y(1),4-y(1)]; % Detect height = 0
isterminal = [1,1]; % Stop the integration
direction = [-1,1]; % Negative and positive directions
end
Jordan Watts
am 18 Jul. 2022
Bearbeitet: Jordan Watts
am 18 Jul. 2022
Antworten (0)
Kategorien
Mehr zu Interactive Control and Callbacks finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!