1

I've been informed what statements we need to know and here are my answers to what they do. Please correct me if I'm mistaken somewhere and please tell me how I learn more advanced statement that I might need e.g. the .* and .^ for element-wise operation I did not yet learn.

%  Mini-Matlab:
%  =============
% 
%  Find out what these statements do!
%  Then you can write a Matlab-program.

% "%" starts a Matlab comment

x=[2 3]

The above is like an array, a vector that resides in memory after the statement.

y=[4 5]'

This is a column vector.

A=[x; y']

The semicolon separates the rows of a matrix.

B=sin(A)

This is like a regular mathematical function.

z=x.*y'

This too is just like a function in the classic math / CS.

c=A\y

This is important and solves an equation system but which? How is the original equation stated? Does the above solve Ay=c? Please specify.

a='Hej'

The above is just a variable like in any other computer language.

a(2)='a'

The above sets the second element of the vector a to the string 'a'.

v=3:2:9

The above constructs a vector from 3 to 9 with the step 2.

w=5:3:15

The above constructs a vector from 5 to 15 with the step 3.

t=0:0.05:10;

t becomes a vector between 0 and 10 with increments of 0.05.

f=t.*exp(2*t);

This is just like a regular function definition.

plot(t,f)

This plots the function on the y-axis and the t on the x-axis.

title('Fin kurva');

This just sets the title of the graph.

g=3*t.*sin(t);

This is a more comlex function definition, still easy to understand what it does.

k=cos(t+2)+sin(t.^2);

Another simple function definition.

help XXX % XXX=sin, cos, plot, disp

Accessing the online help system.

if CONDITION
  STATEMENTS...
end;

Self-explanatory.

if CONDITION
  STATEMENTS...
else
  STATEMENTS...
end;

Also self-explanatory.

for VARIABLE = VECTOR
  SATSER...
end;

A regular for-loop

while VILLKOR
  SATSER...
end;

Easy to understand if you ever programmed.

So I think I understand most of the statement but from this list it appears that the most relevant to study is the equation solver c=A\y that I need to practice how to use.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
  • http://www.mathworks.com/help/techdoc/ref/mldivide.html – Dan Aug 08 '12 at 06:33
  • 1
    For learning more there are some great video tutorials http://blogs.mathworks.com/videos/, also the linked blogs of matlab employees give great examples. – bdecaf Aug 08 '12 at 07:30

2 Answers2

1

x=[2 3]

The above is like an array, a vector that resides in memory after the statement.

[ ... ] creates a matrix of everything you put inside the brackets. A matrix can be scalar (1x1), row-vector (1xN), column-vector (Nx1) or full blown 2d matrix (MxN). Matlab knows also higher dimensional matrices, but you can't input them using the bracket notation (inputting slices (which themselves are 2d) of a higher dimensional matrix is possible however).

y=[4 5]'

This is a column vector.

[4 5] creates a 1x2 vector, the ' is the transpose operator, which transforms it to a 2x1 vector.

A=[x; y']

The semicolon separates the rows of a matrix.

indeed, and because x is 1x2 and y' also, A is 2x2 with its first row x and 2nd row y'.

B=sin(A)

This is like a regular mathematical function.

sin: Y = sin(X) returns the circular sine of the elements of X. So you can input either a scalar, vector, or even a matrix. Output is same size as input.

z=x.*y'

This too is just like a function in the classic math / CS.

.* is the element-wise multiplication operator. It is important that x and y are same size, else this will error! each element of x is multiplied with the corresponding element of y, in this case, the result will be: [2*4 3*5]

c=A\y

This is important and solves an equation system but which? How is the original equation stated? Does the above solve Ay=c? Please specify.

Documentation: Backslash or matrix left division. If A is a square matrix, A\B is roughly the same as inv(A)*B, except it is computed in a different way.

So it is equal to c=inv(A)*y, hence it solves A*c=y. Those slash-and-backslash are always tricky if you don't use them all the time.

a='Hej'

The above is just a variable like in any other computer language.

Incorrect, any text specified within ' symbols, is interpreted as text, so a will be a character array, you can see this using the whos function.

a(2)='a'

The above sets the second element of the vector a to the string 'a'.

Correct, this is only feasible because a is of type char, and 'a' (rhs) is a 1x1 char. The resulting a will contain 'Haj'.

v=3:2:9

The above constructs a vector from 3 to 9 with the step 2.

Correct, so the result is: v = [3 5 7 9]. Notice that 3:2:10 produces the same vector!

w=5:3:15

The above constructs a vector from 5 to 15 with the step 3.

Correct

t=0:0.05:10;

t becomes a vector between 0 and 10 with increments of 0.05.

from 0 to 10, including!

f=t.*exp(2*t);

This is just like a regular function definition.

same thing as with the sine function plus there is a element-wise multiplication of t with exp(...)

plot(t,f)

This plots the function on the y-axis and the t on the x-axis.

This plots the vector f on the y-axis and t on the x-axis, so resulting plot will consist of datapoints (t(ii), f(ii)) with ii ranging from 1 to the length of the vectors. t and f must be of same length!

title('Fin kurva');

This just sets the title of the graph.

Correct

g=3*t.*sin(t); k=cos(t+2)+sin(t.^2);

Simple functions.

Correct

help XXX % XXX=sin, cos, plot, disp

Accessing the online help system.

correct, but it's not online, it's builtin. For example: 'help sin' will give you some help on using the sin function.

if CONDITION STATEMENTS... end;

Self-explanatory.

if CONDITION STATEMENTS... else STATEMENTS... end;

Also self-explanatory.

for VARIABLE = VECTOR SATSER... end;

A regular for-loop

yes, VARIABLE will take all values in VECTOR sequentially. You can pass VECTOR either as a variable, or directly as 1:2:10. Examples: here

The online matlab documentation usually can give all the information you need, and for everything else there is SO.

Gunther Struyf
  • 11,158
  • 2
  • 34
  • 58
1

c=A\y is matrix left division to solve the equation Ac=y.

If A is a square matrix, then A\y is equivalent to inv(A)*y, pinv(A)*y or mldivide(A,y), but they are computed in different ways.

If A is a rectangular matrix, then inv is not applicable, so A\y is equivalent topinv(A)*y and mldivide(A,y). They are the least squares solutions of Ac=y but in different sense.

More info here

chaohuang
  • 3,965
  • 4
  • 27
  • 35