![Untitled.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/253483/Untitled.png)
Graphing Confidence Intervals with upper and lower bounds
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Samantha Chin
el 7 de Dic. de 2019
Respondida: Thiago Henrique Gomes Lobato
el 8 de Dic. de 2019
I want to plot an upper bound and a lower bound confidence interval like this one with a mean line. How can I do this? Right now mine is making about 495 different figures. How would I be able to make this![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/253459/image.jpeg)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/253459/image.jpeg)
this is my code below:
close all; clearvars
mx1= 146.4
sx1= sqrt(12.1)
for n = 5:500
n2 = n-1
CI95_Lower = [mx1-(tinv(0.975,n2)*((sx1)/sqrt(n)))]
CI95_Upper = [mx1+(tinv(0.975,n2)*((sx1)/sqrt(n)))]% separate these so that you can graph it on one graph
t95 = (tinv(0.975,n2)*((sx1)/sqrt(n)))
x_1 = [n];
y1 = [CI95_Lower];
y2 = [CI95_Upper];
hold on
figure ()
plot(x_1,y1,'-', 'MarkerFaceColor', 'b') %plots the confidence intervals
line([0,500],[mx1,mx1])
plot(x_1,y1,'-', 'MarkerFaceColor', 'm')
hold off
end
0 comentarios
Respuestas (1)
Thiago Henrique Gomes Lobato
el 8 de Dic. de 2019
You were creating a picture for every loop iteration, and the plot function can't interpolate between points if you give them one at the time. Saving all the intervals in the looping and only them plot solves your problem
close all; clearvars
mx1= 146.4;
sx1= sqrt(12.1);
y1 = zeros(496,1);
y2 = zeros(496,1);
x_1 = zeros(496,1);
for n = 5:500
n2 = n-1;
CI95_Lower = [mx1-(tinv(0.975,n2)*((sx1)/sqrt(n)))];
CI95_Upper = [mx1+(tinv(0.975,n2)*((sx1)/sqrt(n)))];% separate these so that you can graph it on one graph
t95 = (tinv(0.975,n2)*((sx1)/sqrt(n)));
x_1(n-4) = n;
y1(n-4) = CI95_Lower;
y2(n-4) = CI95_Upper;
end
figure ()
hold on
plot(x_1,y1,'-', 'color', 'r') %plots the confidence intervals
line([0,500],[mx1,mx1],'color','y')
plot(x_1,y2,'-', 'color', 'b')
hold off
![Untitled.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/253483/Untitled.png)
0 comentarios
Ver también
Categorías
Más información sobre Specifying Target for Graphics Output en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!