![Untitled.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/253483/Untitled.png)
Graphing Confidence Intervals with upper and lower bounds
9 次查看(过去 30 天)
显示 更早的评论
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 个评论
回答(1 个)
Thiago Henrique Gomes Lobato
2019-12-8
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Specifying Target for Graphics Output 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!