Effacer les filtres
Effacer les filtres

How to plot a function using a variable that is calculated over a range?

3 vues (au cours des 30 derniers jours)
George
George le 24 Fév 2024
Commenté : Star Strider le 24 Fév 2024
Hello there,
I am new to plotting in MATLAB and am interested in plotting a function using a variable that is calculated over a range. I am getting an error that my matrix dimensions must agree. x is a matrix that is increasing from a to b with 2000 entries. I want to calculate a y that has the same number of entries as x and where x is used in the calculation. Once y is calculated I am interested in plotting y(x).
a = 1.7;
b = 1.3;
c = 0.9;
L = 2000; % Matrix size (1x2000)
l = 0 : (L-1);
x = b + l*((a-b)/L);
y = (c/(2*pi*(sqrt((a*a)-(x.^2))))*(2*atan(sqrt((x.^2)-(b*b))/sqrt((a*a)-(x.^2)))));
figure
plot(x,y,'Color',[0,0.7,0.9])
title('2-D Line Plot')
xlabel('x')
ylabel('y')

Réponse acceptée

Star Strider
Star Strider le 24 Fév 2024
When in doubt, vectorise every multiplication, exponention, and division.
With those changes (and nothing else) it works —
a = 1.7;
b = 1.3;
c = 0.9;
L = 2000; % Matrix size (1x2000)
l = 0 : (L-1);
x = b + l*((a-b)/L);
y = (c./(2*pi*(sqrt((a*a)-(x.^2)))).*(2*atan(sqrt((x.^2)-(b*b))./sqrt((a*a)-(x.^2)))));
figure
plot(x,y,'Color',[0,0.7,0.9])
title('2-D Line Plot')
xlabel('x')
ylabel('y')
I vectorised everything not already vectorised except a*a and b*b, since they obviously do not need vectorisation.
.
  2 commentaires
George
George le 24 Fév 2024
Thats a great rule of thumb to vectorize every operation. Thank you very much for the help!
Star Strider
Star Strider le 24 Fév 2024
As always, my pleasure!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by