Plotting in Matlab with Range for Independent Variable
4 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Matlab12345
le 19 Déc 2019
Réponse apportée : Star Strider
le 19 Déc 2019
I'm creating a simple model of a spring-mass system and am trying to plot the amplitude as a function of the spring constant. I'm using the equation natural frequency = s=sqrt(k/m), where k is the spring constant and m is the mass. Also, amplitude = sqrt(natural frequency^2 + initial position^2)/natural frequency.
I'm not getting any errors, but I'm getting an empty plot and an "answer" in the command window that I do not need.
The code I have so far is:
function A = amp(k)
g = 4; %initial position
m = 2; %mass
k = 0.05:0.1:0.3; %range of spring constant
A = sqrt((sqrt(k/m)).^2+16)/(sqrt(k/m));
figure(3)
plot(k,A);
end
I'd appreciate it if someone could take a closer look at the code.
0 commentaires
Réponse acceptée
Star Strider
le 19 Déc 2019
You need to use element-wise operations in the division:
A = sqrt((sqrt(k/m)).^2+16)./(sqrt(k/m));
↑
Try this:
g = 4; %initial position
m = 2; %mass
k = 0.05:0.01:0.3; %range of spring constant
A = sqrt((sqrt(k/m)).^2+16)./(sqrt(k/m));
figure(3)
plot(k,A);
I also increased the number of elements in ‘k’.
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Assembly dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!