Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm trying create a function that returns a matrix containing a variable "l" which is an independent variable to be swept for a plot later on.

I would calculate "phi" based on user inputs which include "n" and "d", then I would use "n", "d", and "phi" to find "a", "b", "c", and "d" to create a matrix "m" with. This matrix "m" will be a function of "l".

phi = 2*pi*n*d/l;
a = cos(phi);
b = 1i*sin(phi)/n;
c = 1i*n*sin(phi);
d = cos(phi);
m = [a b;c d];

I'm really not enjoying Matlab's coding style as compared to C++ and Python... How would you guys implement this functionality?

Summary: I want a function that returns a matrix which contains an independent variable to be swept for a plot later.

share|cite|improve this question
This question may be better suited for stackoverflow. – Michael Ulm Feb 4 '11 at 7:57
Okay, I'll try over there. – user6605 Feb 4 '11 at 16:41

Just to clearing out of the unanswered list!

You can define either a handle with @ or you can wrap up what you did as a function simply by

function [ mout ] = m_of_l( lin )
phi = 2*pi*n*d/lin ;
mout = [cos(phi) 1i*sin(phi)/n;1i*n*sin(phi) cos(phi)];

Later, you can use M = m_of_l(2*pi) to evaluate it at $l=2\pi$.

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.