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

附加练习结果:

相关推荐
吱吱鼠叔6 小时前
MATLAB数据文件读写:2.矩阵数据读取
数据库·matlab·矩阵
橙意满满的西瓜大侠7 小时前
matlab入门学习(二)矩阵、字符串、基本语句、函数
matlab
kuan_li_lyg9 小时前
MATLAB - 机械臂手眼标定(眼在手内) - 估计安装在机器人上的移动相机的姿态
开发语言·人工智能·matlab·机器人·ros·机械臂·手眼标定
感谢地心引力10 小时前
【MATLAB2024b】安装离线帮助文档(windows)
windows·matlab
通信仿真实验室13 小时前
(4)MATLAB生成CRC校验码
开发语言·数据结构·matlab
月白风清江有声13 小时前
关于KKT条件的线性约束下非线性问题-MATLAB
开发语言·算法·matlab
柠檬少少开发13 小时前
基于MATLAB的安全帽检测系统
matlab
科研工作站13 小时前
MATLAB|电气互联系统有功-无功协同优化模型
matlab·综合能源·电气·有功·无功·电气互联
kuan_li_lyg13 小时前
MATLAB - 机械臂手眼标定(眼在手外) - 估算固定相机相对于机器人基座的姿态
开发语言·人工智能·matlab·机器人·ros·机械臂·手眼标定
WW、forever15 小时前
MATLAB工具库:数据统计分析工具MvCAT、MhAST等
开发语言·matlab