# Matlab 教學(二)
> 此篇內容為還沒有接觸matlab的初學者所寫,作者也是新手,資料來源於網路,有錯的話還請指正
> [Matlab入門教學(一)](https://hackmd.io/5N3j2unnQA-SS53txfTsXg?both)
[Matlab入門教學(三)](https://hackmd.io/fr2bmUY9TcKub2LG9J6_7w?view)
## Equation

```python=
#script
clc, clearvars, close all
x = linspace(0,5);
y = (-(x-3) .^2)+10
plot(x,y)
max(y)
[MaxVal,I] = max(y)
x_maxval = x(I)
```
* 0~5中取出100個值,帶入方程式中
* 印出圖形並找出最大值

* 透過Max function找出y在I=60時有最大值
* 可反推在I=60時,x的值為2.9798

```python=
#script
clc, clearvars, close all
x = linspace(0,5);
y = @(x) (-(x-3) .^2)+10
y(20.7)
x(2)
```
* anonymous function:將整個y宣告為x變數的方程式
* x=20.7時,y的值為-303.2900;x(2)為矩陣,此時的0.0505為第二個矩陣元素內的值,並非運算後的結果

## Plotting

```python=
#script1
clc, clearvars, close all
x = linspace(-10,10);
y1 = (-(x-3) .^2) +10;
y2 = (-(x-3) .^2) +15;
y3 = (-(x-5) .^2) +10;
figure(1)
plot(x,y1,'ms')
xlabel('x'),ylabel('y'),title('Y vs. X - Problem A')
grid on
hold on
plot(x,y2,'bv')
hold on
plot(x,y3,'g+')
legend('y1','y2','y3')
```
* 先準備三個方程式,方便作圖。呼叫```figure(1)```當作作圖介面
* ```plot(x,y)```印出圖形,除了xy軸之外,後方增加的是繪出的線型ex.'ms'為粉色方形的意思,可透過```doc plot```或```help plot```尋找相關編號
* ```title```、```xlabel```、```ylabel```分別為圖的名稱、x軸內容、y軸內容。
:::danger
label必定在plot的後方
:::
* ```grid on```分割背景方格;```hold on ```保留上個線型

```python=
#script2
clc, clearvars, close all
x = linspace(-10,10);
y1 = (-(x-3) .^2) +10;
y2 = (-(x-3) .^2) +15;
y3 = (-(x-5) .^2) +10;
figure(1)
subplot(2,2,1)
plot(x,y1,'--m','MarkerFaceColor','m','MarkerSize',10)
xlabel('x'),ylabel('y'),title('Y vs. X - Problem A')
grid on
subplot(2,2,2)
plot(x,y2,'bv')
xlabel('x'),ylabel('y'),title('Y vs. X - Problem A')
subplot(2,2,3)
plot(x,y3,'gs','MarkerFaceColor','g')
xlabel('x'),ylabel('y'),title('Y vs. X - Problem A')
xlim([0 2]),ylim([-20 100])
```
* ```subplot(x,y,z)```x乘y的大小,z表示第幾位。ex.subplot(2,2,3):在2x2的4個位子中排第三位(左下角)
* ```xlim```、```ylim```限制圖形所打印出的範圍

## Logic

```python=
#script1
% Matrices and vector
clc, clearvars, close all
% parameters
max_x=10;
x = linspace(0,max_x,100000);
y = sin(x);
y_check = 0.8;
% actions
plot(x,y,'*'),hold on ,plot([0 max_x],[y_check y_check],'-r')
y_greater = y > y_check;
% outputs
sum(y_greater)
FinalPercent = sum(y_greater) / length(y) *100
```
* ```linspace```給出範圍內的x值;```y_check```定義比大小的y值
* ```plot1```印出sin(x)的圖形;```plot2```印出想要界定大小,方便觀察
* ```y_greater```實際上為一個等同取樣數量大小的矩陣,將```y > y_check```的值存入,成立存1、不成立存0。
ex.此處的取樣大小為100000,便是1x1000000的矩陣
* ```sum(y_greater)```因為矩陣內只有0跟1,將矩陣元素全部相加後,出來的值為超過0.8的總數,再除以取樣總數,得出百分比

:::info
以下在command window中執行,可一行一行做,觀察執行結果
:::
```python=
A = ones(1,10)
A > 0
A = ones(1,10)*5
A > 0
sum(A)
result = A > 0
sum(result)
8 >= 8
18 + 5 < 100
55 ~= 20
10 > 5 & 11 > -100
10 > 5 & 11 < -100
10 > 5 | 11 < -100
```
* ```&```:and(且)
* ```~=```:does not equal(不等於)
* ```|```:or(或)
## 附錄:電腦很笨,所以人要很聰明
大家應該都聽過看過下面這張梗圖,電腦真的這麼笨嗎?
電腦的確很笨,笨到只能一個口令一個動作,你給他A只會跳出A來。電腦資料庫收到長得像貓生物辨別為狗,那電腦看到的貓就會認為是狗。電腦拓展了人腦的極限,幫我們省略許多步驟,讓我們能夠專注在問題上,嫌棄電腦,很多時候只是找不到正確的使用說明而已,這也是我寫下這篇網誌的原因,希望能夠幫助大家學習如何使用matlab

---
參考資料
>1.[MATLAB Crash Course for Beginners](https://youtu.be/7f50sQYjNRA?si=tSxig57n6B59pO08)