MATLAB教程_台大郭彦甫(14课) 全十四课 每节课的练习题记录

MATLAB教程_台大郭彦甫(14课) 全十四课 每节课的练习题记录

第六节课

```html
close all;
clear all;
x1 = 1:0.01:2;
x2 = 1:0.01:2;
y1 = x1.^2;
y2 = sin(2*pi*x2);
plot(x1,y1,'-b',x2,y2,'or');
legend('x^{2}','sin(2\pix)');
xlabel('Time(ms)');
ylabel('f(t)');
title('Mini Assignment#1');
h = plot(x1,y1);
get(gca);
clear all;
close all;
t = (1:1:4)'*pi/2;
x = sin(t);
y = cos(t);
fill(x,y,'y');
axis square off;
text(0,0,'WAIT','Color','k','Fontsize',60,'FontWeight','bold','HorizontalAlignment','center');
%'Fontsize',60 字体大小;'FontWeight','bold' 字体粗度;'HorizontalAlignment','center'
%字体居于正中心
[x,y] = meshgrid(-3:.2:3,-3:.2:3);
z=x.^2+x.*y+y.^2;
surf(x,y,z);
box on;
set(gca,'FontSize',16);
zlabel('z');
xlim([-4 4]);
xlabel('x');
ylim([-4 4]);
ylabel('y');
colormap(jet);

imagesc(z);
axis square;
xlabel('x');
ylabel('y');
clear all;
close all;
x = -2:1:2;
y = -2:1:2;
[X,Y] = meshgrid(x,y);

第七节课

clear all;
close all;
guide;
function varargout = P7_test1_1(varargin)
% P7_TEST1_1 MATLAB code for P7_test1_1.fig
%      P7_TEST1_1, by itself, creates a new P7_TEST1_1 or raises the existing
%      singleton*.
%
%      H = P7_TEST1_1 returns the handle to a new P7_TEST1_1 or the handle to
%      the existing singleton*.
%
%      P7_TEST1_1('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in P7_TEST1_1.M with the given input arguments.
%
%      P7_TEST1_1('Property','Value',...) creates a new P7_TEST1_1 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before P7_test1_1_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to P7_test1_1_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help P7_test1_1

% Last Modified by GUIDE v2.5 23-Sep-2022 22:09:56

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @P7_test1_1_OpeningFcn, ...
                   'gui_OutputFcn',  @P7_test1_1_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before P7_test1_1 is made visible.
function P7_test1_1_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to P7_test1_1 (see VARARGIN)

% Choose default command line output for P7_test1_1
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes P7_test1_1 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = P7_test1_1_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)


% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton3 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
clear all;
close all;
guide;
function varargout = P7_test1_2(varargin)
% P7_TEST1_2 MATLAB code for P7_test1_2.fig
%      P7_TEST1_2, by itself, creates a new P7_TEST1_2 or raises the existing
%      singleton*.
%
%      H = P7_TEST1_2 returns the handle to a new P7_TEST1_2 or the handle to
%      the existing singleton*.
%
%      P7_TEST1_2('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in P7_TEST1_2.M with the given input arguments.
%
%      P7_TEST1_2('Property','Value',...) creates a new P7_TEST1_2 or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before P7_test1_2_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to P7_test1_2_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help P7_test1_2

% Last Modified by GUIDE v2.5 24-Sep-2022 09:10:57

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @P7_test1_2_OpeningFcn, ...
                   'gui_OutputFcn',  @P7_test1_2_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT


% --- Executes just before P7_test1_2 is made visible.
function P7_test1_2_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to P7_test1_2 (see VARARGIN)

% Choose default command line output for P7_test1_2
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes P7_test1_2 wait for user response (see UIRESUME)
% uiwait(handles.figure1);


% --- Outputs from this function are returned to the command line.
function varargout = P7_test1_2_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;


% --- Executes on slider movement.
function slider1_Callback(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
a = get(handles.slider1,'Value');
b = int16(a);
c = get(handles.slider2,'Value');
d = int16(c);
set(handles.text2,'String',num2str(b+d));

% --- Executes during object creation, after setting all properties.
function slider1_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider1 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on slider movement.
function slider2_Callback(hObject, eventdata, handles)
% hObject    handle to slider2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns position of slider
%        get(hObject,'Min') and get(hObject,'Max') to determine range of slider
a = get(handles.slider1,'Value');
b = int16(a);
c = get(handles.slider2,'Value');
d = int16(c);
set(handles.text2,'String',num2str(b+d));

% --- Executes during object creation, after setting all properties.
function slider2_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider2 (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end

第八节课

clear all;
close all;
I = imread('pout.tif');
I2 = histeq(I);
subplot(1,4,1);
imhist(I);
subplot(1,4,2);
imshow(I);
subplot(1,4,3);
imhist(I2);
subplot(1,4,4);
imshow(I2);
clear all;
close all;
I = imread('rice.png');
subplot(1,2,1);
imshow(I);

%%
%进行旋转遍历
x = 30;   %旋转角度
z = [cos(x) -sin(x);sin(x) cos(x)];
for i = 1:size(I,1)
    for j = 1:size(I,2)
        a = z*[i;j];
        b = a(1);
        c = a(2);
        d = int16(b);
        e = int16(c);
        I(i,j) = I(d,e);
    end
end
subplot(1,2,2);
imshow(I);

第九节课

clear all;
close all;
I = imread('rice.png');
subplot(1,4,1);
imshow(I);
subplot(1,4,2);
imhist(I);
level = graythresh(I);
bw = im2bw(I,level);
subplot(1,4,3);
imshow(bw);

for i = 1:size(I,1)
    for j = 1:size(I,2)
        if  I(i,j)>120
            I(i,j) = 255;
        else
            I(i,j) = 0;
        end
    end
end
subplot(1,4,4);
imshow(I);
clear all;
close all;
I = imread('rice.png');
subplot(3,2,1);
imshow(I);
BG = imopen(I,strel('disk',15));
subplot(3,2,2);
imshow(BG);
I2 = imsubtract(I,BG);
subplot(3,2,3);
imshow(I2);
level = graythresh(I2);
bw = im2bw(I2,level);
subplot(3,2,4);
imshow(bw);
[labeled,numObjects] = bwlabel(bw,8);

RGB_label = label2rgb(labeled);
subplot(3,2,5);
imshow(RGB_label);

第十节课

clear all;
close all;
a = [9 -5 3 7];
x = -2:0.01:5;
f = polyval(a,x);   %画多元函数图
plot(x,f,'LineWidth',2);
xlabel('x');
ylabel('f(x)');
set(gca,'FontSize',14);

P = [5 0 -2 0 1];
polyder(P);  %求微分
b = polyval(polyder(P),7);
clear all;
close all;
a = [20 -7 5 10];
b = [4 12 -3];
x = -2:0.01:1;
m = conv(a,b);
f = polyval(m,x);
z = polyder(m);
f1 = polyval(z,x);
plot(x,f,'-b',x,f1,'-r','LineWidth',2);
xlabel('x');
set(gca,'FontSize',14);
legend('f(x)','f’(x)');
clear all;
close all;
p = [5 0 -2 0 1];
polyint(p,3);
polyval(polyint(p,3),7);
clear all;
close all;
x0 = pi/2;
h = 0.1;
for i = 1:7
    x = [x0 x0+h];
    y = [sin(x0) sin(x0+h)];
    m(i) = diff(y)./diff(x);
    h = h/10;
end
clear all;
close all;
g = colormap(lines);
hold on;
for i = 1:3
    x = 0:power(10,-i):2*pi;
    y = exp(-x).*sin(x.^2/2);
    m = diff(y)./diff(x);
    plot(x(1:end-1),m,'Color',g(i,:));   %画出3条误差线,在h=0.1,h=0.01,h=0.001情况下
end
hold off;
set(gca,'XLim',[0,2*pi]);
set(gca,'YLim',[-0.3,0.3]);
set(gca,'FontSize',18);
set(gca,'XTick',0:pi/2:2*pi);
set(gca,'XTickLabel',{'0','\pi/2','\pi','3\pi/2','2\pi'});
h = legend('h=0.1','h=0.01','h=0.001');
set(h,'FontName','Times New Roman');
box on;
clear all;
close all;
x = 0:0.01:2*pi;
xy_plot(@sin,x);  %对于这种定义的sin,需要加@

y = @(x) 1./(x.^3-2*x-5);
integral(y,0,2);

第十一节课

clear all;
close all;
syms x;
y = cos(x)^2-sin(x)^2;
solve(y,x);

m = cos(x)^2+sin(x)^2;
a = solve(m,x);

%方程式求根
clear all;
close all;

syms x y a b r;
B = solve((x-a)^2+(y-b)^2 - r^2);

syms a b c d;
A = [a b;c d];
C = inv(A);   %求逆矩阵
clear all;
close all;
syms x;
y = exp(x^2)/(x^3-x+3);
g = (x^2+x*y-1)/(y^3+x+3);
A = diff(y);
B = diff(g);
clear all;
close all;
syms x;
y = (x^2-x+1)/(x+3);
z = int(y,x,0,10);
clear all;
close all;
syms x y;
fun = @root2d;       %定义输入变量F1,F2
x0 = [-5 -5];    
A = fsolve(fun,x0);
clear all;
close all;
f = @(x)x.^2;      %输入是x,f是x^2
options = optimset('MaxIter',1e3,'TolFun',1e-10);    %MaxIter是算迭代次数1e3是10^3,TolFun是误差小于多少,1e-10是10^-10,
a = fsolve(f,0.1,options);     
b = fzero(f,0.1,options);      %由于x^2,零点相切所以找出答案是NaN

c = roots([1 -3.5 2.75 2.125 -3.875 1.25]);  %求高项次方程式的解

第十二节课

clear all;
close all;
A = [1 2 1;2 6 1;1 1 4];
b = [2;7;3];
R = rref([A b]);      %对多个线性方程组矩阵化并化简
clear all;
close all;
syms R1 R2 R3 R4 R5 i1 i2 i3 i4 i5 V1 V2
A = [R1 0 0 1 0 ; 0 -R2 0 -1 1 ; 0 0 -R3 0 -1 ; -R4 R4 0 -1 0 ; 0 -R5 R5 0 -1 ; 1 0 0 0 0 ; 0 0 -1 0 0];
B = [0;0;0;0;0;0;0];
x = A\B;

第十四节课

clear all;
close all;
syms R1 R2 R3 R4 R5 i1 i2 i3 i4 i5 V1 V2
A = [R1 0 0 1 0 ; 0 -R2 0 -1 1 ; 0 0 -R3 0 -1 ; -R4 R4 0 -1 0 ; 0 -R5 R5 0 -1 ; 1 0 0 0 0 ; 0 0 -1 0 0];
B = [0;0;0;0;0;0;0];
x = A\B;
clear all;
close all;
x = [0 0.25 0.75 1.25 1.5 1.75 1.875 2 2.125 2.25];
y = [1.2 1.18 1.1 1 0.92 0.8 0.7 0.55 0.35 0];
x1 = linspace(0,2.25,10);
y1 = interp1(x,y,x1);
y2 = spline(x,y,x1);
subplot(1,2,1);
plot(x,y,'bo',x1,y1,'r-');
subplot(1,2,2);
plot(x,y,'bo',x1,y2,'k--');
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Is_LiuYiZheng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值