WICHTIG: Der Betrieb von goMatlab.de wird privat finanziert fortgesetzt. - Mehr Infos...

Mein MATLAB Forum - goMatlab.de

Mein MATLAB Forum

 
Gast > Registrieren       Autologin?   

Partner:




Forum
      Option
[Erweitert]
  • Diese Seite per Mail weiterempfehlen
     


Gehe zu:  
Neues Thema eröffnen Neue Antwort erstellen

Probleme mit Output

 

grandmasta

Gast


Beiträge: ---
Anmeldedatum: ---
Wohnort: ---
Version: ---
     Beitrag Verfasst am: 18.01.2019, 08:41     Titel: Probleme mit Output
  Antworten mit Zitat      
Hallo liebe Community,

ich als Anfänger programmiere leider ziemlich umständlich und versuche mich seit kurzer Zeit es zu verbessern. Bei meinem Beispiel hab ich Probleme mit meinem Output. Ich würde gerne im Qui-Quadrat und Run-Test jeweils die Zahlen bekommen die akzeptiert oder verworfen werden.

Leider bekomme ich bei beiden Tests nur die Zahlen die akzeptiert werden und auch leider bei beiden Tests genau die gleichen Zahlen wie beim anderen, obwohl diese komplett anders berechnet werden. Kann mir bitte jemand helfen wie ich das richtig machen kann??

Vielen Dank für eure Hilfe.

LG

Code:
%%Settings
clc; clear all;

%% Headline
disp('Zufallszahlenüberprüfung')
fprintf('\n')

%% Input

n = 100;                                                % amount of random numbers
x0 = input('Bitte Startwert eingeben: ');
a = input('Bitte Faktor eingeben: ');              
b = input('Bitte Wert für die Verschiebung eingeben: ');                
m = input('Bitte Modulo eingeben: ');                
runs = 0;
significance = 0.9500;                                                    % level of significance of 95 percent
probabilities = [0.005,0.01,0.025,0.0500,0.1,0.5,0.9,0.95,0.975,0.99,0.995];      
degrees_Of_freedom_table = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];  
chi = [0.00,0.00,0.00, 0.00, 0.02, 0.45, 2.71, 3.84, 5.02, 6.63, 7.88; ...         % chi-quadrat distribution table
       0.01,0.02,0.05, 0.10, 0.21, 1.39, 4.61, 5.99, 7.38, 9.21,10.60; ...
       0.07,0.11,0.22, 0.35, 0.58, 2.37, 6.25, 7.81, 9.35,11.34,12.84; ...
       0.21,0.30,0.48, 0.71, 1.06, 3.36, 7.78, 9.49,11.14,13.28,14.86; ...
       0.41,0.55,0.83, 1.15, 1.61, 4.35, 9.24,11.07,12.83,15.09,16.75; ...
       0.68,0.87,1.24, 1.64, 2.20, 5.35,10.64,12.59,14.45,16.81,18.55; ...
       0.99,1.24,1.69, 2.17, 2.83, 6.35,12.02,14.07,16.01,18.48,20.28; ...
       1.34,1.65,2.18, 2.73, 3.49, 7.34,13.36,15.51,17.53,20.09,21.95; ...
       1.73,2.09,2.70, 3.33, 4.17, 8.34,14.68,16.92,19.02,21.67,23.59; ...
       2.16,2.56,3.25, 3.94, 4.87, 9.34,15.99,18.31,20.48,23.21,25.19; ...
       2.60,3.05,3.82, 4.57, 5.58,10.34,17.28,19.68,21.92,24.73,26.76; ...
       3.07,3.57,4.40, 5.23, 6.30,11.34,18.55,21.03,23.34,26.22,28.30; ...
       3.57,4.11,5.01, 5.89, 7.04,12.34,19.81,22.36,24.74,27.69,29.82; ...
       4.07,4.66,5.63, 6.57, 7.79,13.34,21.06,23.68,26.12,29.14,31.32; ...
       4.60,5.23,6.26, 7.26, 8.55,14.34,22.31,25.00,27.49,30.58,32.80; ...
       5.14,5.81,6.91, 7.96, 9.31,15.34,23.54,26.30,28.85,32.00,34.27; ...
       5.70,6.41,7.56, 8.67,10.09,16.34,24.77,27.59,30.19,33.41,35.72; ...
       6.26,7.01,8.23, 9.39,10.86,17.34,25.99,28.87,31.53,34.81,37.16; ...
       6.84,7.63,8.91,10.12,11.65,18.34,27.20,30.14,32.85,36.19,38.58; ...
       7.43,8.26,9.59,10.85,12.44,19.34,28.41,31.41,34.17,37.57,40.00];

 for i = 1:n
   calc_rand = a * x0 + b;
   calc_rand = mod(calc_rand, m);
   x0 = calc_rand;
   random_nr(i) = calc_rand/m;                      % to receive real random numbers we devide with m                        
end  

r = sqrt(n);                                        % r = sqrt(n) -> so we get about 10 separate sub quantities

% parameter for chi-quadrat test
npi = n * (1/r);                                
probability_npi = 1/r;

class_limit_low = 0;                                       % lower limit of a class intervall
class_limit_high = probability_npi;                        % upper limit of a class intervall
numbers_in_intervall = zeros(1,r);                         % counts the quantity of random numbers in one class

for i = 1:r
    for j = 1:n
        if (class_limit_low <= random_nr(j)) && (random_nr(j) < class_limit_high)    % which random numbers are in which class
            numbers_in_intervall(i) = numbers_in_intervall(i) + 1;                   % count the quantity of random numbers in one class
        end
    end
    class_limit_low = class_limit_high;                         % for the next intervall set the lower limit of the class equal to the uppper limit                                         % lower limit of the next class is upper limit of the class before
    class_limit_high = class_limit_high+probability_npi;        % the new upper limit is the old upper limit + probability of npi
end

chi_quadrat = 0;
for k = 1:length(numbers_in_intervall)                                       % from 1 to the amount of numbers in class
    chi_quadrat = sum((numbers_in_intervall(k) - npi)^2)/npi;  % sum of x0 = ((n-np)^2)/np
end

% receive chi quadrat from the table
degrees_Of_freedom_r = r-1;                                          
degrees_Of_freedom = degrees_Of_freedom_table(degrees_Of_freedom_r);      % degrees of freedom in the table

helper_for_probability = find(probabilities == significance, 1, 'last');

probability = probabilities(helper_for_probability);                  % probability
chi_quadrat_table = chi(degrees_Of_freedom, helper_for_probability);  % chi quadrat from the table

% compare the calculated chi quadrat and the qui quadrat from the table
fprintf('\n');
fprintf('Ergebnis des Chi-Quadrat-Test:');
fprintf('\n');
if chi_quadrat <= chi_quadrat_table
    random_nr_accepted = random_nr;
    fprintf('Die angeführten Zahlen werden mit einem Signifikanzniveau von %g Prozent akzeptiert:', significance*100);
    fprintf('\n');
    disp(random_nr_accepted');
else
    fprintf('Die angeführten Zahlen werden mit einem Signifikanzniveau von %g Prozent verworfen:', significance*100);
    fprintf('\n');
    random_nr_rejected = random_nr;
    disp(random_nr_rejectet');
end


% Run-Test

random_nr_run = random_nr;                           % same random numbers for the run test
counter_length = 0;                                  % counts the length
for m = 1:n-1                                        % compare each random number with the following
    if random_nr_run(m) < random_nr_run(m+1)         % if the following random number is bigger
        counter_length = counter_length + 1;         % add one to the counter
    else                                                  
        length_run(m) = counter_length;              
        counter_length = 0;                          % we put the counter to 0
    end
end

max_length_run = max(length_run);                        
for m = 1:max_length_run                                
    counter_runs = 0;                                % counter for each length is zero at the start
    for n = 1:length(length_run)                     % from 1 to length of length_run
        if length_run(n) == m                        % compare all amounts of runs and counts them togehter
            counter_runs = counter_runs + 1;         % add one to the counter
            runs(:,m) = counter_runs;                % adds all sumed up amounts of runs to a vector from 1 to max_length
        end
    end
end

n_run = sum(runs);                                   % quantity of runs

for o = 1:max_length_run                                % from 1 to max_length_run
    pi_run(o) = (1/prod(1:(o))) - (1/prod(1:(o+1)));    % formula P = sum((1/l!)-(1/(l+1!)))
    npi_run(o) = n_run*pi_run(o);                        
end


chi_quadrat_run = 0;
for p = 1:max_length_run                                
    chi_quadrat_run = chi_quadrat_run + ((runs(p) - npi_run(p))^2)/npi_run(p);  % x0 = sum((((n-npi)^2)/npi)
end

for x = 1:length(probabilities)                  
    if probabilities(x) == significance          % when p = the significance
       helper_for_probability_run = x;           % this is the location for the probability
    end  
end

degrees_Of_freedom_run = (max_length_run - 1);              
probability_run = probabilities(helper_for_probability_run);
chi_quadrat_table_run = chi(degrees_Of_freedom_run, helper_for_probability_run);  


% compare the calculated chi quadrat for the run test and the qui quadrat from the table
fprintf('\n');
fprintf('Run-Test:\n');
if chi_quadrat_run <= chi_quadrat_table_run
    random_nr_run_accepted = random_nr_run;
    fprintf('Die angeführten Zahlen werden mit einem Signifikanzniveau von %g Prozent akzeptiert: \n', significance);
   
    disp(random_nr_run_accepted');
else
    random_nr_run_rejected = random_nr_run;
    fprintf('Die angeführten Zahlen werden mit einem Signifikanzniveau von %g Prozent verworfen: \n', significance);
    disp(random_nr_run_rejected');
end
 


Harald
Forum-Meister

Forum-Meister


Beiträge: 24.495
Anmeldedatum: 26.03.09
Wohnort: Nähe München
Version: ab 2017b
     Beitrag Verfasst am: 18.01.2019, 10:26     Titel:
  Antworten mit Zitat      
Hallo,

chi_quadrat und chi_quadrat_table sind wohl Vektoren / Matrizen?
Dann ist der Code aus zwei Gründen nicht sinnvoll
Code:
if chi_quadrat <= chi_quadrat_table
    random_nr_accepted = random_nr;
else
    random_nr_rejected = random_nr;

1. Die Abfrage liefert nur ein true/false zurück
2. Die Zuweisung ist in beiden Fällen die gleiche, nur an eine andere Variable.

Ich kann nur raten, dass du vielleicht das gemeint hast:
Code:
random_nr_accepted = random_nr(chi_quadrat <= chi_quadrat_table)
random_nr_rejected = random_nr(chi_quadrat > chi_quadrat_table)


Grüße,
Harald
_________________

1.) Ask MATLAB Documentation
2.) Search gomatlab.de, google.de or MATLAB Answers
3.) Ask Technical Support of MathWorks
4.) Go mad, your problem is unsolvable ;)
Private Nachricht senden Benutzer-Profile anzeigen
 
Neues Thema eröffnen Neue Antwort erstellen



Einstellungen und Berechtigungen
Beiträge der letzten Zeit anzeigen:

Du kannst Beiträge in dieses Forum schreiben.
Du kannst auf Beiträge in diesem Forum antworten.
Du kannst deine Beiträge in diesem Forum nicht bearbeiten.
Du kannst deine Beiträge in diesem Forum nicht löschen.
Du kannst an Umfragen in diesem Forum nicht mitmachen.
Du kannst Dateien in diesem Forum posten
Du kannst Dateien in diesem Forum herunterladen
.





 Impressum  | Nutzungsbedingungen  | Datenschutz | FAQ | goMatlab RSS Button RSS

Hosted by:


Copyright © 2007 - 2024 goMatlab.de | Dies ist keine offizielle Website der Firma The Mathworks

MATLAB, Simulink, Stateflow, Handle Graphics, Real-Time Workshop, SimBiology, SimHydraulics, SimEvents, and xPC TargetBox are registered trademarks and The MathWorks, the L-shaped membrane logo, and Embedded MATLAB are trademarks of The MathWorks, Inc.