簡體   English   中英

如何在Matlab中在正態分布圖上細化y軸?

[英]How do I refine the y axis on a normal distribution plot in Matlab?

我的Matlab函數幾乎可以完成我想做的事情。 它采用等級向量並生成包括正態分布圖的統計信息。 問題在於,y軸似乎無法反映獲得該年級學生的頻率。

正態分布圖的圖像

我到處都在堆棧溢出中尋找任何遇到我的問題的人。 我什么都找不到。

function [M,m,ave,med,dev,v1]=GradeStatistics
% This program accepts grades and gives the maximum, minimum,average(mean), 
% median, standard deviation and also sorts the grades for the user. 
% This program was written by Jacob
clc;clear;close all;format compact;help GradeStatistics;
disp('Enter a vector containing all grades using square brackets')
v=input('Please give me the grades=');
M=max(v);m=min(v);ave=mean(v);med=median(v);dev=std(v);
v1=sort(v);H={'max';'min';'mean';'median';'std'};
G={M;m;ave;med;dev};clc;disp(table(H,G));
All={'max',M;'min',m;'mean',ave;'median',med;'std',dev};
xlswrite('mygrades.xlsx',All);VV1={'sort',v1};
xlswrite('mygrades.xlsx',VV1,1,'A6');
xlswrite('mygrades.xlsx',v1,1,'B6')

disp('Would you like to see a normal distribution?')
Case = input('Enter Y for yes or N for No=','s');
if lower(Case)=='y'
    f=(1/(dev*sqrt(2*pi)))*exp(-0.5*((v1-ave)/dev).^2);
    hold on;plot(v1,f);title('Normal Distribution of Grades')
    xlabel('Score');ylabel('Students')
elseif lower(Case)=='n'
    disp('Thank you for using GradeStatistics')
end

誰能告訴我如何獲得能更准確地反映坡度矢量的y軸? 還請告訴我是否還有其他我需要改進的功能。

示例等級向量:[0 10 20 23 25 30 35 45 50 53 55 56 58 60 62 65 68 73 74 75 78 80 83 85 90 93 95 98 100]

代碼調整

好的。 首先,讓我們整理一下這段代碼。

clc;clear;close all;format compact;help GradeStatistics;

不要做大部分的事情。 函數開始時永遠不需要clear :所有函數自動從一個新的工作空間開始。 clcformat compact應該留給用戶去做:您不知道他們的顯示首選項是什么。 並在用戶需要時留下help以供用戶致電。 另外,請避免close all :您不知道用戶是否有一些想要保留的數字!

除非您在同一軸上進行多個繪圖,否則就不需要調用hold了。 因此,刪除通話中的hold on通話。

接下來,讓我們重新格式化一下代碼。 將每個語句放在一行上。 由於調試器設置斷點並基本上一次運行一行,因此這使得使用Matlab調試器更具可讀性,並且更易於調試。 為了讓可讀性更好,我們在標記之間留一些空格。

function [M,m,ave,med,dev,v1] = GradeStatistics
% This program accepts grades and gives the maximum, minimum,average(mean), 
% median, standard deviation and also sorts the grades for the user. 
% This program was written by Jacob
disp('Enter a vector containing all grades using square brackets')
v=input('Please give me the grades=');
M=max(v);
m=min(v);
ave=mean(v);
med=median(v);
dev=std(v);
v1=sort(v);
H={'max'; 'min'; 'mean'; 'median'; 'std'};
G={M; m; ave; med; dev};
disp(table(H, G));
All={'max',M; 'min',m; 'mean',ave; 'median',med; 'std',dev};
xlswrite('mygrades.xlsx', All);
VV1={'sort', v1};
xlswrite('mygrades.xlsx', VV1, 1, 'A6');
xlswrite('mygrades.xlsx', v1, 1, 'B6');

disp('Would you like to see a normal distribution?')
Case = input('Enter Y for yes or N for No=','s');
if lower(Case) == 'y'
    f=(1/(dev*sqrt(2*pi)))*exp(-0.5*((v1-ave)/dev).^2);
    plot(v1, f);
    title('Normal Distribution of Grades')
    xlabel('Score');
    ylabel('Students')
elseif lower(Case) == 'n'
    disp('Thank you for using GradeStatistics');
end

修復情節

我不確定您要問的是什么,因為您沒有說確切的Y軸/ Y值出了什么問題。 但是,既然您說您希望Y軸“反映接受該年級學生的頻率”,這聽起來好像您想要的是直方圖而不是線圖? plot()函數產生線圖。

% Display Histogram
figure
histogram(v, 10);
title('Distribution of grades');

GradeStatistics直方圖示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM