Effacer les filtres
Effacer les filtres

what could be causing matrix dimensions must agree error

2 vues (au cours des 30 derniers jours)
justin stephens
justin stephens le 13 Jan 2019
Commenté : Star Strider le 13 Jan 2019
I am trying to plot the gain as a function of frequency. i am trying to vary the frequency from 1 to 1E6, this is so i gan have a frequency range. This is not necessarily needed to solve my problem though. I am getting this error "Error using " / " Matrix dimensions must agree." at line 7 for this segment of code. Its nothing complicated i am just sure that i am doing something incorrect. I have commented out the other way i was going to go about this. so that can be ignored.
clear;
%for i from 0 to 1E8;
% f [i]= i;
c = 3^8;
frequency = 1:100:10^6;
lambda = c/frequency;
area = .1^2;
gain = (((4*pi)/lambda^2)/(area));
subplot (1,1,1)
plot (frequency,gain)
grid on

Réponse acceptée

Star Strider
Star Strider le 13 Jan 2019
You need to use element-wise operations (the ‘dot operator’) in the exponentiation and division, in your lambda and gain calculations.
This works:
c = 3^8;
frequency = 1:100:10^6;
lambda = c./frequency;
area = .1^2;
gain = (((4*pi)./lambda.^2)/(area));
subplot (1,1,1)
plot (frequency,gain)
grid on
See the documentation on Array vs. Matrix Operations (link) for an extended discussion.
  2 commentaires
justin stephens
justin stephens le 13 Jan 2019
awesome! thank you! I am just not a matlab guru yet and didn't really know about that.
Star Strider
Star Strider le 13 Jan 2019
As always, my pleasure!
No worries!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by