Top Banner
軟軟軟軟軟軟軟 1 Roots of nonlinear functions Nested FOR loops Lecture 5II
40

軟體實作與計算實驗 1 Roots of nonlinear functions Nested FOR loops Lecture 5II.

Dec 14, 2015

Download

Documents

Kyla Hilyer
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 1

Roots of nonlinear functionsNested FOR loops

Lecture 5II

Page 2: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 2

Nonlinear system

0

04

21

2

2

2

1

xx

xx

Page 3: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 3

A set of nonlinear functions

21212

22

21211

211

21121

),(

4),(

),(

),(),F(

xxxxf

xxxxf

xxf

xxfxx

Page 4: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 4

function F = myfun(x) F(1) = x(1).^2 + x(2)^2-4; F(2) = x(1) - x(2);return

Function evaluation

Page 5: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 5

x=linspace(-2,2);plot(x,x); hold on; plot(x,sqrt(4-x.^2))plot(x,-sqrt(4-x.^2))

Page 6: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 6

Least square of nonlinear functions

i

ixxxxf 2

21,),(min

21

0),(

04),(

SOLVE

21212

22

21211

xxxxf

xxxxf

Page 7: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 7

x0= ones(1,2)*2;x = lsqnonlin(@myfun,x0)

Find a zero

y=myfun(x);sum(y.^2)

CHECKING

Page 8: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 8

Multiple roots

x0= ones(1,2)*2;x = lsqnonlin(@myfun,x0)v=[v;x];plot(x(1),x(2),'o');

v=[]

for i=1:n

demo_lsq.m

Page 9: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 9

012),(

0242),(

SOLVE

22212

22

21211

21

xxxxf

xxxxf

Page 10: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 10

function F = myfun2(x) F(1) = 2*x(1).^2 + x(2)^2-24; F(2) = x(1).^2 - x(2).^2+12;return

Function evaluation

012),(

0242),(22

212

22

21211

21

xxxxf

xxxxf

Page 11: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 11

x0= ones(1,2)*2;x = lsqnonlin(@myfun2,x0)

Find a zero

y=myfun2(x);sum(y.^2)

CHECKING

Page 12: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 12

Multiple roots

x0= ones(1,2)*2;x = lsqnonlin(@myfun2,x0)v=[v;x];plot(x(1),x(2),'o');

v=[]

for i=1:n

Page 13: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 13

x=linspace(-5,5);plot(x,sqrt(24-2*x.^2),'r');hold on; plot(x,-sqrt(24-2*x.^2),'r')plot(x,sqrt(12+x.^2),'b')plot(x,-sqrt(12+x.^2),'b')

Page 14: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 14

Demo_lsq_2c

source code

Page 15: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 15

02),(

022),(

SOLVE

21212

22

21211

xxxxf

xxxxf

Page 16: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 16

function F = myfun3(x) F(1) = x(1).^2 -2* x(2)^2-2; F(2) = x(1).*x(2) -2;return

Function evaluation

02),(

022),(

SOLVE

21212

22

21211

xxxxf

xxxxf

Page 17: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 17

x=linspace(-3,3);x=x(find(abs(x) > 0.1));plot(x,sqrt((x.^2-2)/2),'r');hold on; plot(x,-sqrt((x.^2-2)/),'r')x=linspace(0.5,3);plot(x,2./x,'b');x=linspace(-0.5,-3);plot(x,2./x,'b')

Page 18: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 18

x0= ones(1,2)*2;x = lsqnonlin(@myfun3,x0)

Find a zero

y=myfun3(x);sum(y.^2)

CHECKING

Page 19: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 19

Multiple roots

x0= ones(1,2)*2;x = lsqnonlin(@myfun3,x0)v=[v;x];plot(x(1),x(2),'o');

v=[]

for i=1:n

Page 20: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 20

demo_lsq_hyperbola.txt

two_hyperbola.txt

Page 21: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 21

Matrix Multiplication

C=A*BC(i,j)=A(i,:) *B(:,j) for all i,j

Page 22: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 22

Nested FOR Loops

C(i,j)=A(i,:) *B(:,j)

[n,d1]=size(A);[d2,m]=size(B)

for i=1:n

for j=1:m

Page 23: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 23

[n,d1]=size(A);[d2,m]=size(B);for i=1:nfor j=1:m C(i,j)=A(i, :) *B(:,j);endend

Page 24: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 24

Constrained optimization

13

100

102

22

21

12

2

1

xx

xx

x

x

Inequality constraint

Nonlinear equality

Page 25: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 25

12

2

1

100

102

xx

x

x

222

21

,)13(min

21

xxxx

Constrained optimization

Page 26: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 26

Constraints

Lower bound and upper boundEquality constraintInequality constraint

Page 27: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 27

Constraints

Lower bound

Upper bound

],[]0,2[ 21 xx

]10,10[],[ 21 xx

Inequality constraints

0112

1

12

x

x

xx

Page 28: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 28

Demo_conop2

function demo_conop2()lb =[-2 0];ub =[10,10];Aeq=[-1 1];beq=[0];A=[];b=[];x = fmincon(@fun,[1 1],A,b,Aeq,beq,lb,ub)x(1).^2+x(2).^2returnfunction y = fun(x)y = (x(1).^2+x(2).^2-13).^2;return

demo_conop2.m

Page 29: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 29

function y = fun(x)

y = (x(1).^2+x(2).^2-13).^2;

return

Objective function

Page 30: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 30

Calling fmincon

x = fmincon(@fun,[1 1],A,b,Aeq,beq,lb,ub)

objective function initial

guesslinearequality

inequality

Lower and upperbound

Page 31: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 31

Random variable

Sample space S={A,T,C,G}X is a discrete random variable with

sample space S.

apAX )Pr(

cpCX )Pr(tpTX )Pr(

gpGX )Pr(

Page 32: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 32

Generative model

pa

pt pc

pg

A T C G

Flip-flop

1 gcta pppp

gatcacaggt

Page 33: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 33

Partition

ap0 1cta

ta

a

pppn

ppn

pn

3

2

1

1n 2n 3n

Knots partition [0,1] into four intervals respectively with lengths,

321 and , nnn

gcta pppp and ,,

tp cp gp

Page 34: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 34

r=rand;Tag= (r<=c(1))+ 2*(r<=c(2) & r > c(1))+3*(r<=c(3) & r > c(2))+4*(r<=c(4) & r > c(3))switch Tagcase 1

s=[s 'A'];case 2

s=[s 'T'];case 3

s=[s 'C'];case 4

s=[s 'G'];end

Page 35: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 35

Sequence generation

Emulation of a generative model for creation of an atcg sequence

Page 36: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 36

FOR Loops

r=rand;switch r…end

input p,mn=length(p)p_sum=0;s=[];

for i=1:n

for j=1:m

p_sum=p_sum+p(i);c(i)=p_sum;

Page 37: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 37

Mixture model

pa

pt pc

pg

A T C G

Flip-flop

pa

pt pc

pg

A T C G

Flip-flop1 2

Page 38: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 38

Mixture model

According to probabilities, and

each time one of two joined models is selected to generate a character

Created characters are collected to form an atcg sequence

1 2

Page 39: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 39

Hidden Markov model

1 2

pa

pt pc

pg

A T C G

pa

pt pc

pg

A T C G

11 21

22

12

Page 40: 軟體實作與計算實驗 1  Roots of nonlinear functions  Nested FOR loops Lecture 5II.

軟體實作與計算實驗 40

Transition probability

2221

1211