martes, 15 de marzo de 2016

Medición en colorímetro

Para encontrar el polinomio que se ajusta al promedio de las mediciones presiona aqui:
Ajuste de curvas
PROGRAMA PARA REALIZAR MEDICIONES EN EL COLORÍMETRO

PROGRAMA PARA REALIZAR MEDICIONES EN EL COLORÍMETRO

En este programa se dan las intrucciones para ingresar el nombre de la hoja de excel en que se guardaran y el numero de muestras arroja un promedio.

Contents

Pregunta si quieres empezar

clc
close all
clear all
di=input('Quieres hacer mediciones Si(1) No(2):     ');
if di>2 | di<1
    disp('ERROR VUELVE A INTENTAR...')
    pause(1)
    clc
    di=input('Quieres hacer mediciones Yes(1) No(2):     ');
else if di==2
        pause(1)
        disp('Adios...')
    end
end

Corre el programa

while di==1
Lama a la tarjeta arduino
    a=arduino('COM4');
    disp('Comenzamos!!!')
pwm rojo
    a.pinMode(9, 'OUTPUT');
pwm verde
    a.pinMode(10,'OUTPUT');
pwm azul
    a.pinMode(11,'OUTPUT');
En este archivo se registran las mediciones
    filename='Mediciones.xlsx';
Es el nombre de la hoja de excel donde se almacenan las mediciones
    sheet=input('Escribe el nombre de la hoja :     ','s');
    mu=input('Ingresa el numero de muestras:   ');
    %
    in=0;
    while mu>0
        mu=mu-1;
        in=in+1;
        fprintf('*********    Muestra núm %1.0f \n',in)
        for i=9:11,
            a.digitalWrite(i,1);
        end
        pause(1)

Rojo ON

        a.digitalWrite(9,0)
        pause(0.5)
Normaliza el valor leído con la fotorresistencia
$$ Valor={1023-v/1023}*254 $$
        vr(in,1)=((1023-analogRead(a,0))/1023)*254;
        disp('valor rojo:  ')
        disp(vr)
        a.digitalWrite(9,1)
        pause(.5)

Verde ON

        a.digitalWrite(10,0)
        pause(0.5)
        vv(in,1)=((1023-analogRead(a,0))/1023)*254;
        disp('valor verde:  ')
        disp(vv)
        a.digitalWrite(10,1)
        pause(.5)

Azul ON

        a.digitalWrite(11,0)
        pause(0.5)
        va(in,1)=((1023-analogRead(a,0))/1023)*254;
        disp('valor azul:  ')
        disp(va)
        a.digitalWrite(11,1)
        pause(.5)

Blanco, todos los leds son encendidos

        for i=9:11,
            a.digitalWrite(i,0);
        end
        pause(0.5)
        vb(in,1)=((1023-analogRead(a,0))/1023)*254;
        disp('valor blanco:  ')
        disp(vb)
        pause(.5)

Negro, todos los leds se apagan

        for j=9:11,
            a.digitalWrite(j,1);
        end
        pause(0.5)
        vn(in,1)=((1023-analogRead(a,0))/1023)*254;
        disp('valor negro:  ')
        disp(vn)
    end
Promedia los vectores construidos con el número de muestras
    pr=mean(vr);
    pv=mean(vv);
    pa=mean(va);
    pb=mean(vb);
    pn=mean(vn);
    a={'Prom rojo','Prom verde','Prom azul','Prom blanco','Prom negro'; pr, pv, pa, pb, pn};

Escritura en excel

    xlswrite(filename,vr,sheet,'A1')
    xlswrite(filename,vv,sheet,'B1')
    xlswrite(filename,va,sheet,'C1')
    xlswrite(filename,vb,sheet,'D1')
    xlswrite(filename,vn,sheet,'E1')
    xlswrite(filename,a,sheet,'F1')
    disp('Si quieres ya puedes ver los datos en el archivo Mediciones.xlsx');
Vuelve a preguntar si quieres mas mediciones
    di=input('Quieres hacer mediciones: Yes(1) No(2):   ');
    if di >2 | di<1
        disp('ERROR VUELVE A INTENTAR...')
        pause(1)
        clc
        di=input('Quieres hacer mediciones Si(1) No(2):     ');
    else if di==2
            pause(0.7)
            disp('Adios...')
        end
    end
end
Error using input
Cannot call INPUT from EVALC.

Error in p4 (line 9)
di=input('Quieres hacer mediciones Si(1) No(2):     ');

Ajuste de curvas

INTERPOLACIÓN DE DATOS

INTERPOLACIÓN DE DATOS

Se ingresan los valores medidos de los aguacates durante 6 dias en matrices para su fácil manejo, y se busca un polinomio que se ajuste a la tendencia

Contents

clear all
close all
clc

Aguacate uno

MAT_1=[
    209.03  191.63  192.84  192.07  191.46  187.38 % Blanco
    133.36  105.41  112.92  110.96  110.76  105.38% Azul
    188.81  165.47  165.20  163.79  163.84  156.64% Verde
    187.38  164.81  165.75  166.02  164.39  160.65% Rojo
    20.69   18.57   18.70   17.77   17.44   17.02];% Negro

Agucate dos

MAT_2=[
    206.63  195.98  194.47  189.39  186.99  183.35 % Blanco
    130.77  109.38  110.63  104.20  103.98  96.64 % Azul
    183.95  166.27  161.75  154.13  153.42  147.95 % Verde
    185.91  174.38  173.50  167.63  162.85  159.29 % Rojo
    20.80   19.34   19.31   17.90   17.41  16.33]; % Negro

Aguacate tres

MAT_3=[
    208.31  190.52  199.68  195.32  190.11  185.55 % Blanco
    132.64  108.14  121.72  116.17  110.10  99.10  % Azul
    187.21  162.19  175.37  170.38  162.27  154.77 % Verde
    187.10  163.60  173.80  168.62  163.54  159.59 % Rojo
    20.94   19.01   19.92   18.32   17.63   16.97]; % Negro

Corrección día 3

MAT_1(:,3)=(MAT_1(:,2)+MAT_1(:,4))/2;
MAT_2(:,3)=(MAT_2(:,2)+MAT_2(:,4))/2;
MAT_3(:,3)=(MAT_3(:,2)+MAT_3(:,4))/2;

Porcentaje de cambio en verde

Ag_1=MAT_1(3,:)./(MAT_1(1,:).*MAT_1(5,:));
Ag_2=MAT_2(3,:)./(MAT_2(1,:).*MAT_2(5,:));
Ag_3=MAT_3(3,:)./(MAT_3(1,:).*MAT_3(5,:));
Ag_P=(Ag_1+Ag_2+Ag_3)/3;
dia=[1,2,3,4,5,6];
figure(1)
plot(dia,Ag_P,'--dg','Linewidth',2),title('Comportamiento verde','FontSize',12,'FontWeight','bold'),xlabel('Dias'),ylabel('Valores');

Gráfica de los tres agucates y su promedio

figure(2)
plot(dia,Ag_1,'--*b',...
     dia,Ag_2,'--or',...
     dia,Ag_3,'--pm',...
     dia,Ag_P,'--^g'),title('Cambio de color a traves de los días','FontSize',12,'FontWeight','bold'),xlabel('Dias'),ylabel('Valores');
legend('Aguacate uno ','Aguacate dos ','Aguacate tres ','Promedio','Location','BestOutside');

Polinomio y su ajuste

horas=1:0.1:6;
P son los coeficientes del polinomio de 5to grado S es el grado aproximadoo de error
[P,S]= polyfit(dia,Ag_P,5)
figure(3)
plot(dia,Ag_P,'--^b',...
    horas,polyval(P,horas),'--*g'),title('Ajuste del polinomio','FontSize',12,'FontWeight','bold'),xlabel('Dias'),ylabel('Valores');
legend('Promedio','Polinomio grado 5','Location','BestOutside');
P =

    0.0000   -0.0001    0.0008   -0.0034    0.0078    0.0380


S = 

        R: [6x6 double]
       df: 0
    normr: 6.3216e-17

sábado, 30 de enero de 2016

Testing moderation in the context of ANOVA

Testing a Potential Moderator

 In this case, I am using the ANOVA tool for assessing the following hypothesis:
"There is a relationship between drink beer and be laid off at the last 12 months, as moderator is the situation to have got separated or broken off a relationship"
Quantity response variable=Quantity beer
Categorical explanatory variable=Be laid off or not
Moderator=got separated or broken off a relationship

In the first case when a person has got separated or broken off a relationship, p value =0.0735, that is it is not statistically significantly, for the second case p-value=.0001 it reveals that there is a relationship between the variables.

In general in the media values reveal for both cases either with or without to have got separated or broken relationship, people  who  has been laid off tend to drink more than who has a job. This can see in the group S1Q234(Laid off 1 yes, 2 no)  level  1 is bigger than level 2 in both cases

LIBNAME mydata "/courses/d1406ae5ba27fe300 " access=readonly;
DATA new; set mydata.nesarc_pds;

LABEL ALCABDEP12DX="Alcohol Dependence Past 12 Months"
      S2AQ5A=" Drank any beer Last 12 Months"
      S2AQ5B="How often Drank beer Last 12 Months"
      S2AQ5D="Numbers of beer consumed";
     
     
     /*Set appropriate missing data as needed*/
IF S2AQ5B=99 THEN S2AQ5B=.;
IF S2AQ5D=99 THEN S2AQ5D=.;


IF S2AQ5B=1 THEN FREQMO=365;
ELSE IF S2AQ5B=2 THEN FREQMO=312;
ELSE IF S2AQ5B=3 THEN FREQMO=208;
ELSE IF S2AQ5B=4 THEN FREQMO=104;
ELSE IF S2AQ5B=5 THEN FREQMO=52;
ELSE IF S2AQ5B=6 THEN FREQMO=36;
ELSE IF S2AQ5B=7 THEN FREQMO=12;
ELSE IF S2AQ5B=8 THEN FREQMO=11;
ELSE IF S2AQ5B=9 THEN FREQMO=6;
ELSE IF S2AQ5B=10 THEN FREQMO=2;
/*days per year
365=Every day
312=Nearly every day
208=3 to 4 times a week
104=2 times a week
52=Once a week
36=2 to 3 times a month
12=Once a month
11=7 to 11 times in the last year
6=3 to 6 times in the last year
2=1 or 2 times in the last year*/

NUMBEER=FREQMO*S2AQ5D;

/*subsetting data to include only past 12 month smokers, age 18-25*/
IF S2AQ5A=1;
IF AGE LE 25;

PROC SORT; by IDNUM;
PROC SORT; by S1Q238;
PROC ANOVA; CLASS S1Q234;
MODEL NUMBEER=S1Q234;
MEANS S1Q234;by S1Q238;


RUN;
Procedimiento ANOVA
Información de nivel de clase
Clase Niveles Valores
S1Q234 3 1 2 9
Número de observaciones leídas 494
Número de observaciones usadas 491

Procedimiento ANOVA
Variable dependiente: NUMBEER
Fuente DF Suma de cuadrados Cuadrado de la media F-Valor Pr > F
Modelo 2 3969215.1 1984607.6 2.62 0.0735
Error 488 369085652.9 756323.1
Total corregido 490 373054868.0
R-cuadrado Coef Var Raíz MSE NUMBEER Media
0.010640 201.4764 869.6684 431.6477
Fuente DF Anova SS Cuadrado de la media F-Valor Pr > F
S1Q234 2 3969215.143 1984607.572 2.62 0.0735
Distribución de NUMBEER por S1Q234

Procedimiento ANOVA
Distribución de NUMBEER por S1Q234
Nivel de
S1Q234
N NUMBEER
Media Dev std
1 102 605.029412 1215.06217
2 388 386.994845 753.92456
9 1 72.000000 .

Procedimiento ANOVA
Información de nivel de clase
Clase Niveles Valores
S1Q234 2 1 2
Número de observaciones leídas 2357
Número de observaciones usadas 2348

Procedimiento ANOVA
Variable dependiente: NUMBEER
Fuente DF Suma de cuadrados Cuadrado de la media F-Valor Pr > F
Modelo 1 6406134.2 6406134.2 15.21 <.0001
Error 2346 988331490.7 421283.7
Total corregido 2347 994737624.9
R-cuadrado Coef Var Raíz MSE NUMBEER Media
0.006440 216.3500 649.0637 300.0064
Fuente DF Anova SS Cuadrado de la media F-Valor Pr > F
S1Q234 1 6406134.184 6406134.184 15.21 <.0001
Distribución de NUMBEER por S1Q234

Procedimiento ANOVA
Distribución de NUMBEER por S1Q234


Nivel de
S1Q234
N NUMBEER
Media Dev std

1293438.337884
1079.88294

22055280.283212
561.59831