matlab

用Matlab编写的两个简单程序

1.在屏幕上输出k到N再到k的数列
如:N=8 k=1时,输出1 2 3 4 5 6 7 8 7 6 5 4 3 2 1

程序代码:

function Answer_Of_Programme=test0123210(P1,P2)
k=1;
for i=P1:1:P2
    Answer_Of_Programme(k,:)=i;
    k=k+1;
end
for i=P2-1:-1:P1
    Answer_Of_Programme(k,:)=i;
    k=k+1;
end
运行程序:Answer_Of_Programme=test0123210(P1的值,P2的值)

2.用Matlab编写的一个实现,输出feboca级数的前N项的程序

程序代码:

function Answer_Of_Programme=feibo(N)
k=1;
a1=0;
a2=1;
Answer_Of_Programme(k,:)=a1;
k=k+1;
Answer_Of_Programme(k,:)=a1+a2;
k=k+1;
for i=0:1:N-3
    Answer_Of_Programme(k,:)=a1+a2;
    k=k+1;
    b=a1+a2;
    a1=a2;
    a2=b;
end

运行程序:Answer_Of_Programme=feibo(N的值)

4 thoughts on “matlab

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.