MATLAB Fundamentals>>>(2/2) Project - Analyze Vehicle Data

#创作灵感#

MATLAB基础知识官方课程学习笔记


MATLAB Fundamentals>Common Data Analysis Techniques>Summary of Common Data Analysis Techniques>(2/2) Project - Analyze Vehicle Data


任务名称:Fuel Economy Analysis


任务1:

The variable mpg contains NaN values. Find the rows in mpg with NaN values, and remove those rows from all three data vectors: mpg, hp, and wt.

解答1:

Matlab 复制代码
nanIdx = ismissing(mpg)
mpg = mpg(~nanIdx);
hp = hp(~nanIdx);
wt = wt(~nanIdx);

结果1:


背景:

Fuel economy in the U.S. is typically given in miles/gallon. In many countries, however, the standard units are liters/100km.

Given mpg, you can calculate economy in L/100km by dividing 235.214583 by mpg.

任务2:

Create a variable econ that contains the fuel economy in L/100km rather than miles/gallon.

Combine the data for weight, horsepower, and fuel economy in L/100km (in that order) into a 48-by-3 matrix called numdata.

解答2:

Matlab 复制代码
econ = 235.214583./mpg
numdata = [wt hp econ]

笔记:注意mpg是个向量,需要使用"./"。

结果2:


任务3:

Create a matrix of the scatter plots of the variables in numdata (weight, horsepower, and fuel economy) in a single figure.

Calculate the corresponding correlation coefficients and store them as a matrix called cc.

解答3:

Matlab 复制代码
plotmatrix(numdata)
cc = corrcoef(numdata)

结果3:


任务4:

Determine the best-fit line (i.e., a first degree polynomial fit) for fuel economy (in L/100km) as a function of vehicle weight.

Evaluate the fitted model at the weights in the data. Store the fitted values in a vector called econFit.

Note that you do not need to use centering and scaling for the fit.

解答4:

Matlab 复制代码
c = polyfit(wt,econ,1)
econFit = polyval(c,wt)

结果4:


任务5:

Create a scatter plot of fuel economy against weight, and add the best-fit line as a red line.

解答5:

Matlab 复制代码
scatter(wt,econ)
hold on 
plot(wt,econFit,"r")
hold off

结果5:


附加练习:

Try fitting a line to horsepower as a function of weight. Try plotting both scatter plots and fits together, using yyaxis to accommodate the different scales:

附加练习解答:

Matlab 复制代码
chp = polyfit(wt,hp,1)
hpFit = polyval(chp,wt)
yyaxis right
hold on
scatter(wt,hp)
plot(wt,hpFit)
hold off

附加练习结果:

相关推荐
蓝桉(努力版)5 小时前
MATLAB可视化5:华夫图(饼图的平替可以表示种类的分布,附有完整代码详细讲解)(求个关注、点赞和收藏)(对配色不满意可以自己调节配色,附调色教程)
开发语言·数学建模·matlab·信息可视化·matlab可视化
freexyn6 小时前
Matlab自学笔记六十二:求解三角函数方程的通解周期解
笔记·算法·matlab
DesolateGIS3 天前
数学建模:非线性规划:凸规划问题
数学建模·matlab
IT猿手3 天前
2025最新智能优化算法:沙狐优化(Rüppell‘s Fox Optimizer,RFO)算法求解23个经典函数测试集,完整MATLAB代码
android·算法·matlab·迁移学习·优化算法·动态多目标优化·动态多目标进化算法
rit84324993 天前
MATLAB基于voronoi生成三维圆柱形
开发语言·人工智能·matlab
微光-沫年3 天前
141-CEEMDAN-VMD-Transformer-BiLSTM-ABKDE多变量区间预测模型!
算法·matlab·回归
Akangya3 天前
Matlab-Simulink之步长
matlab
Python大数据分析@5 天前
Origin、MATLAB、Python 用于科研作图,哪个最好?
开发语言·python·matlab
牛马baby5 天前
MATLAB下载安装教程(附安装包)2025最新版(MATLAB R2024b)
开发语言·matlab
Evand J5 天前
【MATLAB例程】AOA与TDOA混合定位例程,适用于三维环境、4个锚点的情况,附下载链接
开发语言·matlab