Matlab数学建模笔记


记录使用Matlab解决数学建模问题中遇到的坑。

数据拟合

[P,S,mu] = polyfit(x,y,n)

  • 给出在最小二乘一一下最佳拟合系数

    • 函数根据采样点x, y,产生一个n次多项式P及其在采样点的误差向量S

    • x, y为等长向量

    • P为长度为m+1的向量

图片X轴过小

1
2
3
4
5
6
7
8
9
10
XX = [170 120 180 200 190 220; 120 100 110 180 170 180; 70 50 80 100 95 120];
[Data, TextData] = xlsread('sj.xlsx');
bar(Data,'stacked')
set(gca,'xtick',1:31,'xticklabel',["北京","天津","河北","山西","内蒙古","辽宁","吉林","黑龙江","上海","江苏","浙江","安徽","福建","江西","山东","河南","湖北","湖南","广东","广西","海南","重庆","四川","贵州","云南","西藏","陕西","甘肃","青海","宁夏","新疆"])
% 使用set命令的'xtick',1:31参数更改X轴显示
legend("食品","衣着","设备","医疗","交通","教育","居住","杂项")
xlabel('不同城市的八个方面');
ylabel('一年的消费情况');
grid on
title('31个城市的不同消费情况')

高斯平滑处理

1
2
3
4
5
6
7
%高斯数据平滑处理,高斯滤波
b = smoothdata(a,"gaussian",500);
x_gj1=b(:,1);
z_gj1=b(:,2);
x_a=a(:,1);
z_a=a(:,2);
plot(x_gj1,z_gj1,x_a,z_a)

评论