Matlab m file and object-oriented programming

Source: Internet
Author: User

One. Matlab Control flow

1.FOR Loop structure:

For I=1:10;
X (i) =i;
End
X
x =
1 2 3 4 5 6 7 8 9 10

2.while Loop structure:

The elements of the Fibonacci array satisfy the Fibonacci rule and now require the first element in the array that is greater than 10000.

A (1) =1;a (2) =1;i=2;
While a (i) <=10000
A (i+1) =a (i-1) +a (i);
I=i+1;end;

I,a (i)

i =
21st
Ans =
10946

3.if-else-end Branching structure

cost=10;number=12;
If number>8
Sums=number*0.95*cost;
End,sums

sums =
114.0000

Use the For loop instruction to seek the first element in the FIBONACC array that is greater than 10000:

N=100;a=ones (1,n);
For I=3:n
A (i) =a (i-1) +a (i-2);
If a (i) >=10000
A (i),
Break
End
End,i

Ans =
10946
i =
21st

4.switch-case structure: Student's performance management, used to demonstrate the application of switch structure

Clear;
%
For I=1:10;a{i}=89+i;b{i}=79+i;c{i}=69+i;d{i}=59+i;end;c=[d,c];
name={' Jack ', ' Marry ', ' Peter ', ' Rose ', ' Tom '};
mark={72,83,56,94,100}; Rank=cell (1,5);
%
s=struct (' name ', name, ' Marks ', Mark, ' rank ', rank);
%
For I=1:5
Switch S (i). Marks
Case
S (i). rank= ' perfect score ';
Case a
S (i). Rank= ' excellent ';
Case B
S (i). rank= ' good ';
Case C
S (i). rank= ' Pass ';

otherwise
S (i). rank= ' fail ';
End
End
%
DISP ([' Student name ', ' score ', ' Rank ']);d ISP (')
For I=1:5;
DISP ([S (i). Name,blanks (6), Num2str (S (i). Marks), blanks (6), S (i). Rank]);
End

Student name score Level
Jack passed 72.
Marry 83 Good
Peter 56 failed.
Rose 94 Excellent
Tom 100 out of

5.try-catch structure: The row of the (3x3) magic Square is invoked, and when the row subscript exceeds the maximum number of rows in the magic square, the invocation of the last line is redirected and an "error" warning is displayed.

clear,n=4; A=magic (3);
Try
A_n=a (N,:)
Catch
A_end=a (end,:)
End
LastErr
A_end =
4 9 2
Ans =
Index exceeds matrix dimensions.

Two. Cross-space variable passing

1. Calculate the value of a string expression across space: (A) write a program that draws regular polygon or circles. (B) The relationship between the sub-function and the (parent) function. (C) A variety of different working spaces. (D) The similarities and differences between Evalin operating mechanism and eval.

(1)
[exm070531_1.m]
function y1=exm070531_1 (a,s)
t= (0:a)/a*2*pi;
y1=subevalinzzy (4,s);
%------------subfunction-------------

t= (0:a)/a*2*pi;ss= ' A*exp (i*t) ';
switch s
case {' base ', ' Caller '}
y2=evalin (S,SS);
case ' self '
y2=eval (ss);
end

(2)
clear,a=30;t= (0:a)/a*2*pi;sss={' base ', ' caller ', ' self '};
For K=1:3
Y0=exm070531_1 (8,sss{k});
Subplot (1,3,K)
Plot (Real (y0), Imag (y0), ' R ', ' LineWidth ', 3), Axis square image
End

2. Cross-space assignment: Assignin operation mechanism demonstration.

(1)
[EXM070532_1.M]
function Y=exm070532_1 (x)
Y=SQRT (x); t=x^2;
Assignin (' base ', ' yy ', T)

(2)
Clear;x=4;y=exm070532_1 (x);
DISP ([Blanks (5), ' X ', blanks (5), ' Y ', blanks (4), ' yy ']), disp ([X,y,yy])
X y yy
4 2 16

Three. String calculus function

1.eval

"Example 7.6.1-1" evaluates the "expression" string, resulting in a vector value.
Clear,t=pi;cem= ' [T/2,t*2,sin (t)] '; Y=eval (CEM)
y =
1.5708 6.2832 0.0000
"Example 7.6.1-2" computes the "statement" string, creating a variable.
Clear,t=pi;eval (' Theta=t/2,y=sin (theta) ');
theta =
1.5708
y =
1

Your variables is:
T Theta y

"Example 7.6.1-3" computes the "substitution" string.
A=ones (2,1); B=ones (1,3); C=eval (' B*a ', ' a*b '), Errmessage=lasterr
c =
1 1 1
1 1 1
Errmessage =
Error using ==> *
Inner matrix dimensions must agree.

"Example 7.6.1-4" computes the "composition" string.
cem={' cos ', ' sin ', ' tan '};
For K=1:3
THETA=PI*K/12;
Y (1,k) =eval ([Cem{1}, ' (', Num2str (theta), ') ']);
End
Y
y =
0.9659 0.8660 0.7071

2.feval

"Example 7.6.2-1" one of the differences between Feval and eval: Feval's FN is definitely not an expression.
X=PI/4; Ve=eval (' 1+sin (x) ')
Ve =
1.7071
Vf=feval (' 1+sin (x) ', x)
??? Error using ==> feval
Invalid function Name ' 1+sin (x) '.
"Example 7.6.2-2" Feval and eval call differences: Feval's FN only accepts function names. In this case, the two methods are good.
RANDN (' seed ', 1); A=rand (2,2);
[Ue,de,ve]=eval (' SVD (A) ');
Disp (' Results by eval ');d ISP ([ue,de,ve]);d ISP (Blanks (1))
[Uf,df,vf]=feval (' SVD ', A);
Disp (' Results by Feval ');d ISP ([UF,DF,VF])
Results by Eval
-0.9193-0.3936 1.2212 0-0.7897-0.6135
-0.3936 0.9193 0 0.2633-0.6135 0.7897
Results by Feval
-0.9193-0.3936 1.2212 0-0.7897-0.6135
-0.3936 0.9193 0 0.2633-0.6135 0.7897

3. Inline functions

Example 7.6.3.3-1: The first form of creation of an inline function, and an inline function for array operations.
Clear,f1=inline (' Sin (rho)/rho ')
F1 =
Inline function:
F1 (rho) = sin (rho)/rho
F1=F1 (2)
F1 =
0.4546
Ff1=vectorize (F1)
XX=[0.5,1,1.5,2];FF1=FF1 (XX)
FF1 =
Inline function:
FF1 (rho) = sin (rho)./rho
FF1 =
0.9589 0.8415 0.6650 0.4546

"Example 7.6.3.3-2" Demo: The first type of inline function creates a flaw in the format; the assignment of multiple inputs with vectors.
G1=inline (' A*exp (x (1)) *cos (x (2)) '), G1 (2,[-1,PI/3])
G1 =
Inline function:
G1 (a) = A*exp (x (1)) *cos (x (2))
??? Error using ==> inline/subsref
Too many inputs to inline function.
G2=inline (' A*exp (x (1)) *cos (x (2)) ', ' a ', ' X '), G2 (2,[-1,PI/3])
G2 =
Inline function:
G2 (a,x) = A*exp (x (1)) *cos (x (2))
Ans =
0.3679

Example 7.6.3.3-3: An inline function that produces a vector input, a vector output, and a method for invoking a vector function.
Y2=inline (' [X (1) ^2;3*x (1) *sin (x (2))] ')
Argnames (Y2)
Y2 =
Inline function:
Y2 (x) = [x (1) ^2;3*x (1) *sin (x (2))]
Ans =
' X '
X=[4,PI/6];
Y2=y2 (x)
y2 =
16.0000
6.0000

"Example 7.6.3.3-4" Demo: Create inline functions in the most concise format; inline functions can be called by feval directives.
Z2=inline (' P1*x*sin (X^2+P2) ', 2)
Z2 =
Inline function:
Z2 (X,P1,P2) = P1*x*sin (X^2+P2)
Z2=Z2 (2,2,3)
Fz2=feval (z2,2,2,3)
Z2 =
2.6279
FZ2 =

2.6279

Matlab m file and object-oriented programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.