The online racing simulator
MATLAB help
(6 posts, started )
MATLAB help
Hey guys,

I've got some issues with MATLAB. My task is to write a function that computes the the determinant of an n x n ( n=1,...,3 ) matrix, without using the det() command.

The problem is that I have no idea how to make a function. Here's what I have, this works fine in case of a 3x3 matrix. Still, it's not a function:

B = (A(1,1) * A(2,2) * A(3,3)) - (A(1,1) * A(3,2) * A(2,3)) - (A(2,1) * A(1,2) * A(3,3)) + (A(2,1) * A(3,2) * A(1,3)) + (A(3,1) * A(1,2) * A(2,3)) - (A(3,1) * A(2,2) * A(1,3))

B is the determinant, A is the matrix and its elements in the brakets. It worked fine, checked it and got the same result with the det() command.
Basically a function in matlab is as follows. This line must be the first line in your .m file:

function [output1, output2, ...] = myFunction (input1, input2, ....)
(and here is the body of the function where you can write any code you want)
.
.
.
end

The last line in the file must be "end". You can have as many outputs and inputs as you want, comma-separated.

Next you need an if statement to check the dimensions of your input matrix.

if expression1
statements1
elseif expression2
statements2
else
statements3
end

Don't forget the "end". Use [m,n] = size(X) to find the size of the input matrix. You need to check for 2x2 and 3x3 matrices, and also make sure m == n. Obviously the determinant of a 1x1 matrix is the only number in the matrix itself. Once you have an if statement for each size of matrix, do the calculation for each one and store the answer in an output variable (you only need 1 in this function). Then go into the main matlab console and call your function like you call any other function.

Also, matlab has a good web site if you need help with the syntax of things like if statements and for loops and so on, you should check it out.
Thanks!
gosh....i didnt understand a single thing
yet, im not a coder.
but reading this i guess i wont ever be
Quote from shiny_red_cobra :Basically a function in matlab is as follows. This line must be the first line in your .m file:

function [output1, output2, ...] = myFunction (input1, input2, ....)
(and here is the body of the function where you can write any code you want)
.
.
.
end

function needs no "end"
Ah you're right, it doesn't. My bad, I hate matlab.

MATLAB help
(6 posts, started )
FGED GREDG RDFGDR GSFDG