[Matlab] practice_3

dewdew·2023년 6월 28일
0

Matlab & Simulink

목록 보기
3/6
post-thumbnail

[Matlab] 3일차 학습

학습 소스 링크 첨부 (11-15)

% built-in function
% unique : 실제로 가질 수 있는 값
% 6 * rand
% ceil(6*rand)
% A = ceil(6*rand(1000,1))
% unique(A)

% matlab programming : main / function

% Data input & output
% a = input('Type a --> ')
% a = input('Type a \n 1:yes \n 0:no \n --> ')
% a = input('Type a --> ')
% a = input('Type your name : ', 's')

% disp() : ans 마저 지워서 표현해주는 명령어
x = rand
x
disp(x)
s = 'hello'
disp(s)
['answer : ', num2str(x), 'rad/s']
disp(['answer : ', num2str(x), 'rad/s'])

% if
a = 3
b = sqrt(3)^2
a == b % logical '0' why? : binary to decimal conversion 할 때 값이 안떨어짐
% format long e
% solution
abs(a-b) < 1e-10
% for
for i = 1 : 10 % row vector [1 2 3 4 ... 10]
    disp(i)
end

% while(condition) condition 이 false 가 될때까지

load handel
whos
Fs
% sound(y, Fs) play
% sound(y, 2*Fs)

% early iteration stop
for i = 1 : 4
    for j = 1 : 5
        disp( [i, j] )
    end
end

% making function
format compact

[aaa, bbb, ccc] = maxij1(a)
b = magic(5)
[aa, bb, cc] = maxij1(b)

% f(x) = 0 을 찾은 algorithm
% mynewton 참고

% local variable
% function 에서 사용된 변수는 기능이 끝나면 모두 사라진다.
a = 20
b = 30
y = fn_local(3) % main 함수에서 사용된 a, b 를 사용하지 않는다.
% memory 에 있는 a, b 값은 function 을 부르기 전값("가명"!)

% glocal variable
global A B C % do not use comma to define multiple global vars
whos A B C
A = 7, B = 3, C = 5;
fn_global(2,3)
A
fn_global(2,3)
A
who global
clear
who
fn_global(2,3) 
% clear => global variable 이 memory workspace 에 저장되어있음
clear global
fn_global(2,3)

% function handle : function 내부에 function input 을 넣고싶을 때 유용
cos(3)
s = 'cos'
s(3)

s = @cos
s(3)
whos s
y = s(3)
y = feval(s, 3) % old ver.


profile
연습장

0개의 댓글