MATLAB Answers

Creating linear equations in matlab

14 views (last 30 days)
How can i create 9 different equations in matlab if i have this general form of equations. I never did any thing of this kind before. I can enter these equations manually but i have very high number of equations (1000) so thats why i am looking for some method which can create it for me. In this case "n" is equal 2. Thank you so much


Sign in to comment.

Accepted Answer

Star Strider
Star Strider on 20 Feb 2016
This is how I would build your matrix:
C = pi; % Assume A Value For ‘C’, A Scalar
n = 3; % Choose ‘n’
P = randi(99, n); % Create ‘P’
for j1 = 2:(n-1)
for j2 = 2:(n-1)
M(j1, j2) = C*P(j1, j2) - C * P(j1+1, j2) - C * P(j1, j2+1) - C * P(j1, j2-1);
j1 = n-1;
for j2 = 2:(n-1)
M(j1, j2) = C*P(j1, j2) - C * P(j1+1, j2) - C * P(j1, j2+1) - C * P(j1, j2-1) - C * P(j1, j2-1);
j1 = n;
j2 = n;
M(j1, j2) = C * P(j1, j2) - C * P(j1-1, j2) - C * P(j1, j2-1);
Note that MATLAB indices begin with 1 (all index references have to be positive integers), so it’s necessary to consider that in the code and make the appropriate changes. If you want to use this in a solver function of some sort, I would wrap the loops in a function and then use that function as the objective function of the solver. There may be more efficient ways to create your matrix, but I decided to just go with the description you provided.
You will likely have to experiment with this to get the result you want, but this should get you started.
While I’m thinking about it, let us know how your communications research goes, and attach PDFs of papers you publish from it, especially those that include MATLAB scripts or functions. Also, if you develop any MATLAB scripts that could be useful for others, document them thoroughly and submit them to the File Exchange.


Show 1 older comment
Star Strider
Star Strider on 20 Feb 2016
With respect to the matrix, bear in mind MATLAB’s indexing conventions, so ‘P(1,0)’ would have to be ‘P(2,1)’. If you need to use the indices to calculate, just subtract 1 from them in your code. Build your matrix like I built the one in your Question. I would use the Optimization Toolbox fsolve function to solve it, so consider how it wants its objective functions coded, and write a function file with your matrix calculations as your objective function. If you want to estimate the ‘P’ matrix, you would have to enter it as a 9-element vector, so for instance if ‘Pv’ is that vector, ‘Pv(1)=P(1,1)’ ... ‘Pv(9)=P(3,3)’, then map it appropriately in your function. If you want to constrain the optimsation function so that the probabilities are bounded (0,1), consider using fmincon or one of the other Optimization Toolbox functions that allow constraints.
You lost me on the second matrix. It’s sparse by definition, so the sparse matrix functions would likely be appropriate to work with it.
If you have functions related to your paper that you want to upload to the File Exchange, be sure to attach it there. Especially if it relates to this Question or another Question, attach it here or to all that are appropriate. (I’d like to add your papers to my publication library) By attaching the paper, you don’t have to then explain in detail the context of your Question.
Aftab Ahmed Khan
Aftab Ahmed Khan on 22 Feb 2016
Ok thank you. Here is my first paper. I will also send you my second paper which is under review. I really appreciate your help.
Star Strider
Star Strider on 22 Feb 2016
As always, my pleasure.
Thank you. I saved it and will look at it later. It’s far from my areas of expertise, so I’ll definitely learn from it.

Sign in to comment.

More Answers (0)