Calculating a matrix with a variable inside

13 vues (au cours des 30 derniers jours)
Mohammadamin Malek Pour
Mohammadamin Malek Pour le 17 Avr 2021
Commenté : Clayton Gotberg le 17 Avr 2021
Hi all,
I'm trying to calculate matrix A for different b's. b is defined as follows:
b = logspace(10,1000,100)
Here's the error that i'm getting:
"Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."
This is what I have and it's clearly not working. Any thoughts or suggestions would be much appreciated.
b = logspace(10,100,1000);
for i = 1:length(b)
A(i) = [exp(b(i))*0.5 , 0;exp(-0.5i*b(i)), b(i)*0.5];
s = det(A)
plot(b,A)
end
  3 commentaires
Matt J
Matt J le 17 Avr 2021
This is what I have and it's clearly not working
Clear from what?
Mohammadamin Malek Pour
Mohammadamin Malek Pour le 17 Avr 2021
@Clayton Gotbergjust updated the question and included the error. thanks

Connectez-vous pour commenter.

Réponses (1)

Clayton Gotberg
Clayton Gotberg le 17 Avr 2021
Modifié(e) : Clayton Gotberg le 17 Avr 2021
The first issue you are having is because you are telling MATLAB to expect a 1x1 answer with A(i) and then providing a 2x2 matrix to fit into that space.
If you need to save 2x2 matrices on each iteration, you can either stack them on top of each other:
A = [0 1 %The first two rows are the results from the first iteration
1 2
3 4 % The second two rows are the results from the second iteration
2 6
... % And so on...
which will cause your indexing to be much more confusing (like A(2*i:2*i+1,:)) or you can use cell arrays, which have indexing more similar to what you are already using and which will make retreiving those matrices much easier later.
A = {[0 1; 1 2],[3 4; 2 6], .. }
% ^ ^ ^
% First iteration 2nd And so on...
To assign to cell arrays, use curly braces around your indexing variable (A{i}). You can later retrieve the data within a cell using curly braces or can retrieve a cell (or set of cells) using brackets.
The second issue you will face is with the det function. Inputs must be square matrices, but A changes size on each iteration and will almost never be square. If this is intended to apply to the matrix from each run, you'll need to change this to address it.
Finally, you need to check that your plot function is in the right place. As it is, you are trying to plot something on each iteration and b and A will usually not be the same size.
  2 commentaires
Mohammadamin Malek Pour
Mohammadamin Malek Pour le 17 Avr 2021
Modifié(e) : Mohammadamin Malek Pour le 17 Avr 2021
Thanks so much for your response.
I think I didn't ask my question clearly. So, what I'm trying to do is to calculate that A matrix for every single b. So for b = 10, I'm gonna have one A matrix, for b = 20 I will have another A matrix, and so on.
And then I'd like to calculate the determinant of A to see at what b's the deretminant would go to infinity.
Thanks again!
Clayton Gotberg
Clayton Gotberg le 17 Avr 2021
I'm gald I could help! In that case, the trouble with your code is that you are trying to save the matrix after every loop, instead of its determinant.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Produits

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by