Matlab基础题总结和习题提示

举报
zhangrelay 发表于 2021/07/15 02:58:01 2021/07/15
【摘要】 安装与使用说明:https://blog.csdn.net/zhangrelay/article/details/79622079 网页版本:https://blog.csdn.net/zhangrelay/article/details/79529824 浓缩指令备忘录和习题提示: 下载链接::https://share.weiyun.com/5XlV19a 密码:ebu...

安装与使用说明:https://blog.csdn.net/zhangrelay/article/details/79622079

网页版本:https://blog.csdn.net/zhangrelay/article/details/79529824

浓缩指令备忘录和习题提示

下载链接::https://share.weiyun.com/5XlV19a 密码:ebu4qp



推荐使用新版Matlab,如2017a、2017b等。

中文官网:http://cn.mathworks.com/

中文论坛:http://www.ilovematlab.cn/forum.php

帮助文档:http://cn.mathworks.com/help/


对应实验1到实验8:


  
  1. Matlab习题提示:
  2. 实验1
  3. %1.1
  4. clc;
  5. %1.2
  6. x0=10;v0=15;a=-9.84;t=5;
  7. x=x0+v0*t+0.5*a*t.^2;
  8. disp(x)
  9. %1.3 ln log() log log10() 180 = pi x.*x x*x
  10. z1=2*sin(pi*85/180)/(1+exp(2))
  11. x=[2 1+2i;-0.45 5]
  12. z2=0.5*log(x+sqrt(1+x^2))
  13. t=0:0.5:2.5
  14. z3=(t>=0&t<1).*(t.^2)+(t>=1&t<2).*(t.^2-1)+(t>=2&t<3).*(t.^2-2*t+1)
  15. %1.4 plot()
  16. x=0:0.1:10;
  17. y=2*exp(-0.2*x);
  18. plot(x,y);
  19. %1.5 exa1 exa2 exa8....
  20. 实验2
  21. %zeros(n)... linspace(1,5,5)... logspace(1,5,5.5)...A[] A()...A.*B
  22. %A*B...rank()...eig(A)...det(A)
  23. %2.1
  24. %2.2
  25. %2.3 x=A\B A*x=B
  26. %2.4
  27. randn(6,6)
  28. %2.5 help rand ...
  29. rand('state',0)
  30. a=rand(2,2)
  31. s1=num2str(a)
  32. ss=sprintf('%.10e\n',a)
  33. fprint('%.5g\\',a)
  34. s_sscan=sscanf(ss,'%f',[3,2])
  35. %2.6
  36. %2.7 P=UI=I2R linspace
  37. Rs=linspace(50,50,1001);Vs=linspace(120,120,1001);
  38. Rl=0:0.1:100;
  39. Il=Vs./(Rs+Rl);
  40. Pl=(Il.*Il).*Rl;
  41. [Plmax,Rlnum]=max(Pl)
  42. plot(Rl,Pl)
  43. 实验3
  44. %plot(),subplot(),figure(),hold on,bar(),
  45. %area(),pie(),hist(),stem(),stairs,plot3(),
  46. %mesh(),surf(),view(),
  47. %3.1 %3.2 %3.3%3.4%3.5%3.6%Q
  48. x=-10:0.1:10;
  49. y=sqrt((64-x.*x)/2.0);
  50. y1=-sqrt((64-x.*x)/2.0);
  51. plot(x,y);
  52. hold on;
  53. plot(x,y1);
  54. t=-10:0.1:10;
  55. x=sin(t);
  56. y=cos(t);
  57. plot(x,y);
  58. y=sin(x.^-1);
  59. plot(x,y);
  60. fplot(x,y); %error
  61. function Y = myfun(x)
  62. Y(:,1) = x(:);
  63. Y(:,2) = sin(x(:).^-1);
  64. fh = @myfun;
  65. fplot(fh,[-2 2])
  66. x=-3:0.1:3;
  67. y=-3:0.1:3;
  68. [X,Y]=meshgrid(x,y);
  69. Z=-X.^2+Y.^2;
  70. figure(1);
  71. mesh(X,Y,Z);
  72. figure(2);
  73. surf(X,Y,Z);
  74. a=1;
  75. x=a*sin(t);
  76. y=sqrt(25-a^2)*cos(t);
  77. subplot(2,2,1)
  78. plot(x,y);
  79. a=2;
  80. x=a*sin(t);
  81. y=sqrt(25-a^2)*cos(t);
  82. subplot(2,2,2)
  83. plot(x,y);
  84. a=3;
  85. x=a*sin(t);
  86. y=sqrt(25-a^2)*cos(t);
  87. subplot(2,2,3)
  88. plot(x,y);
  89. a=4;
  90. x=a*sin(t);
  91. y=sqrt(25-a^2)*cos(t);
  92. subplot(2,2,4)
  93. plot(x,y);
  94. 实验4
  95. 4.1 注意syms x y 与 x=sym('6'); 具体过程如下:
  96. syms x y;
  97. x=sym('6');
  98. y=sym('5');
  99. sprintf('z符号表达式求解结果为:')
  100. z=(x+1)/(sqrt(3+x)-sqrt(y))
  101. 4.2 factor() 参考例题即可
  102. 4.3 simple() 具体示例如下:
  103. syms p1 p2 x
  104. F=sin(p1)*cos(p2)-cos(p1)*sin(p2)
  105. sprintf('F化简结果为')
  106. f=simple(F)
  107. G=(4*x^2+8*x+3)/(2*x+1)
  108. sprintf('G化简结果为')
  109. g=simple(G)
  110. syms x;
  111. f=x^6+1;
  112. s=factor(f)
  113. syms x;
  114. f=x^2-1;
  115. s=factor(f)
  116. syms h n x;
  117. L=limit((log(x+h)-log(x))/h,h,0)
  118. M=limit((1-x)/n^n,n,inf)
  119. N=limit((1-x/n)^n,n,inf)
  120. syms a x;
  121. y=sin(a*x);
  122. A=diff(y,x)
  123. B=diff(y,a)
  124. C=diff(y,x,2)
  125. syms n;
  126. S=symsum(1/n^2,1,inf)
  127. S1=symsum(1/n^2,1,10)
  128. S2=symsum(n,1,100)
  129. syms x;
  130. f=x*sin(x);
  131. F=fourier(f)
  132. syms x y;
  133. x=sym('6');
  134. y=sym('5');
  135. z=(x+1)/(sqrt(3+x)-sqrt(y))
  136. syms x1 x2;
  137. f=sin(x1)*cos(x2)-cos(x1)*sin(x2);
  138. p=simple(f)
  139. syms x y;
  140. f=x^4-y^4;
  141. s=factor(f)
  142. syms x1 x2;
  143. syms p1 p2 x
  144. F=sin(p1)*cos(p2)-cos(p1)*sin(p2)
  145. sprintf('F化简结果为')
  146. f=simple(F)
  147. G=(4*x^2+8*x+3)/(2*x+1)
  148. sprintf('G化简结果为')
  149. g=simple(G)
  150. syms x y;
  151. x=sym('6');
  152. y=sym('5');
  153. sprintf('z符号表达式求解结果为:')
  154. z=(x+1)/(sqrt(3+x)-sqrt(y))
  155. 实验5:选择结构(if else)示例有错误。
  156. 5.1 if else习题
  157. word=input('请输入一个字符,','s')
  158. if word>='A'&word<='Z'
  159. sprintf('是大写字母啊!!!')
  160. char(word+1)
  161. elseif word>='a'&word<='z'
  162. sprintf('是小写字母啊!!!')
  163. char(word-1)
  164. elseif word>='1'&word<='9'
  165. sprintf('是数字啊!!!')
  166. char(word)
  167. else
  168. sprintf('都是些什么啊!!!')
  169. char(word)
  170. end
  171. 5.2 switch case习题
  172. word=input('请输入字符串,','s')
  173. switch word
  174. case 'Monday'
  175. disp('1')
  176. case 'Sunday'
  177. disp('7')
  178. case 'Happy'
  179. disp('666')
  180. otherwise
  181. disp('Unknown')
  182. end
  183. 5.3 参考如下:
  184. A=[2 2 2;3 4 3];B=[1 2;3 2;6 6];
  185. try
  186. sprintf('1')
  187. C=A*B
  188. catch
  189. sprintf('2')
  190. C=A.*B
  191. end
  192. 5.4
  193. a=20;b=-2;c=0;d=1;
  194. disp('a>b'),a>b
  195. disp('b>d'),b>d
  196. disp('a>b&c>d'),a>b&c>d
  197. disp('a==b'),a==b
  198. disp('a&b>c'),a&b>c
  199. disp('~~b'),~~b
  200. 5.5 类似5.4
  201. 思考题
  202. s=0;
  203. a=2;
  204. b=1;
  205. t=0;
  206. for i=1:16
  207. s=s+a./b;
  208. t=a;
  209. a=a+b;
  210. b=t;
  211. end
  212. s
  213. 实验六
  214. 具体参考指导书
  215. 实验七
  216. 效果图:
  217. 代码(请慎重使用全局变量):
  218. function varargout = ex7gui(varargin)
  219. % EX7GUI M-file for ex7gui.fig
  220. % EX7GUI, by itself, creates a new EX7GUI or raises the existing
  221. % singleton*.
  222. %
  223. % H = EX7GUI returns the handle to a new EX7GUI or the handle to
  224. % the existing singleton*.
  225. %
  226. % EX7GUI('CALLBACK',hObject,eventData,handles,...) calls the local
  227. % function named CALLBACK in EX7GUI.M with the given input arguments.
  228. %
  229. % EX7GUI('Property','Value',...) creates a new EX7GUI or raises the
  230. % existing singleton*. Starting from the left, property value pairs are
  231. % applied to the GUI before ex7gui_OpeningFunction gets called. An
  232. % unrecognized property name or invalid value makes property application
  233. % stop. All inputs are passed to ex7gui_OpeningFcn via varargin.
  234. %
  235. % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
  236. % instance to run (singleton)".
  237. %
  238. % See also: GUIDE, GUIDATA, GUIHANDLES
  239. % Copyright 2002-2003 The MathWorks, Inc.
  240. % Edit the above text to modify the response to help ex7gui
  241. % Last Modified by GUIDE v2.5 28-Mar-2018 14:02:01
  242. % Begin initialization code - DO NOT EDIT
  243. gui_Singleton = 1;
  244. gui_State = struct('gui_Name', mfilename, ...
  245. 'gui_Singleton', gui_Singleton, ...
  246. 'gui_OpeningFcn', @ex7gui_OpeningFcn, ...
  247. 'gui_OutputFcn', @ex7gui_OutputFcn, ...
  248. 'gui_LayoutFcn', [] , ...
  249. 'gui_Callback', []);
  250. if nargin && ischar(varargin{1})
  251. gui_State.gui_Callback = str2func(varargin{1});
  252. end
  253. if nargout
  254. [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
  255. else
  256. gui_mainfcn(gui_State, varargin{:});
  257. end
  258. % End initialization code - DO NOT EDIT
  259. % --- Executes just before ex7gui is made visible.
  260. function ex7gui_OpeningFcn(hObject, eventdata, handles, varargin)
  261. % This function has no output args, see OutputFcn.
  262. % hObject handle to figure
  263. % eventdata reserved - to be defined in a future version of MATLAB
  264. % handles structure with handles and user data (see GUIDATA)
  265. % varargin command line arguments to ex7gui (see VARARGIN)
  266. % Choose default command line output for ex7gui
  267. handles.output = hObject;
  268. global BB;
  269. % Update handles structure
  270. guidata(hObject, handles);
  271. % UIWAIT makes ex7gui wait for user response (see UIRESUME)
  272. % uiwait(handles.figure1);
  273. % --- Outputs from this function are returned to the command line.
  274. function varargout = ex7gui_OutputFcn(hObject, eventdata, handles)
  275. % varargout cell array for returning output args (see VARARGOUT);
  276. % hObject handle to figure
  277. % eventdata reserved - to be defined in a future version of MATLAB
  278. % handles structure with handles and user data (see GUIDATA)
  279. % Get default command line output from handles structure
  280. varargout{1} = handles.output;
  281. % --- Executes on button press in pushbutton1.
  282. function pushbutton1_Callback(hObject, eventdata, handles)
  283. % hObject handle to pushbutton1 (see GCBO)
  284. % eventdata reserved - to be defined in a future version of MATLAB
  285. % handles structure with handles and user data (see GUIDATA)
  286. %figure(1);
  287. AA = str2double(get(handles.edit1,'String'));
  288. global BB
  289. x=-10:0.1:10;
  290. %plot(x,sin(x)+cos(x)+x)
  291. line(x,AA*(sin(BB*x)+cos(BB*x))+x)
  292. % --- Executes on button press in pushbutton2.
  293. function pushbutton2_Callback(hObject, eventdata, handles)
  294. % hObject handle to pushbutton2 (see GCBO)
  295. % eventdata reserved - to be defined in a future version of MATLAB
  296. % handles structure with handles and user data (see GUIDATA)
  297. cla;
  298. % --- Executes on button press in pushbutton3.
  299. function pushbutton3_Callback(hObject, eventdata, handles)
  300. % hObject handle to pushbutton3 (see GCBO)
  301. % eventdata reserved - to be defined in a future version of MATLAB
  302. % handles structure with handles and user data (see GUIDATA)
  303. function edit1_Callback(hObject, eventdata, handles)
  304. % hObject handle to edit1 (see GCBO)
  305. % eventdata reserved - to be defined in a future version of MATLAB
  306. % handles structure with handles and user data (see GUIDATA)
  307. % Hints: get(hObject,'String') returns contents of edit1 as text
  308. % str2double(get(hObject,'String')) returns contents of edit1 as a double
  309. %AA = str2double(get(hObject,'String'))
  310. % --- Executes during object creation, after setting all properties.
  311. function edit1_CreateFcn(hObject, eventdata, handles)
  312. % hObject handle to edit1 (see GCBO)
  313. % eventdata reserved - to be defined in a future version of MATLAB
  314. % handles empty - handles not created until after all CreateFcns called
  315. % Hint: edit controls usually have a white background on Windows.
  316. % See ISPC and COMPUTER.
  317. if ispc
  318. set(hObject,'BackgroundColor','white');
  319. else
  320. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  321. end
  322. % --- Executes on button press in radiobutton1.
  323. function radiobutton1_Callback(hObject, eventdata, handles)
  324. % hObject handle to radiobutton1 (see GCBO)
  325. % eventdata reserved - to be defined in a future version of MATLAB
  326. % handles structure with handles and user data (see GUIDATA)
  327. % Hint: get(hObject,'Value') returns toggle state of radiobutton1
  328. if get(hObject,'Value')
  329. grid on;
  330. else
  331. grid off;
  332. end
  333. % --- Executes on slider movement.
  334. function slider2_Callback(hObject, eventdata, handles)
  335. % hObject handle to slider2 (see GCBO)
  336. % eventdata reserved - to be defined in a future version of MATLAB
  337. % handles structure with handles and user data (see GUIDATA)
  338. % Hints: get(hObject,'Value') returns position of slider
  339. % get(hObject,'Min') and get(hObject,'Max') to determine range of slider
  340. global BB
  341. BB=50*get(hObject,'Value')+1
  342. % --- Executes during object creation, after setting all properties.
  343. function slider2_CreateFcn(hObject, eventdata, handles)
  344. % hObject handle to slider2 (see GCBO)
  345. % eventdata reserved - to be defined in a future version of MATLAB
  346. % handles empty - handles not created until after all CreateFcns called
  347. % Hint: slider controls usually have a light gray background, change
  348. % 'usewhitebg' to 0 to use default. See ISPC and COMPUTER.
  349. usewhitebg = 1;
  350. if usewhitebg
  351. set(hObject,'BackgroundColor',[.9 .9 .9]);
  352. else
  353. set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
  354. end
  355. --
  356. 实验八:
  357. simulink
  358. 注意模块一定要确保准确,参数正确。

----~~~~----







文章来源: zhangrelay.blog.csdn.net,作者:zhangrelay,版权归原作者所有,如需转载,请联系作者。

原文链接:zhangrelay.blog.csdn.net/article/details/79729736

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。