How can I make linspace work in the case where I want to plot a function
Show older comments
I want to plot the maximum theta for acceleration(af) where acceleration is in a range between 0.4905,49.05.
How can I do this ? This script is only using 49.05 as af, and not as a linear vector. When plotting it gives me a single point.
this is the function
function theta2dot=pendulumcode2(t,theta,g,d,af,kg)
theta2dot=zeros(1,1);
theta2dot=(af*d*cos(theta)-g*d*sin(theta))/(kg^2+d^2);
this is the script
clc;clear
g=9.81;
kg=0.26; d=0.46;
tspan=[0,6];
theta0=[0;0];
N=300;
for af=linspace(0.4905,49.05,N)
[t,theta]=ode45(@(t,theta)pendulumcode2(t,theta,g,d,af,kg),tspan,theta0);
end
[maxt,maxtheta]=max(theta);
c2=max(theta);
y2=t(maxtheta);
degree2 = c2(1,1);
degree = degree2*180/pi
time = y2(1,1)
figure;
hold on
plot(degree,af(:,1));
xlabel('theta')
ylabel('af')
hold off
Answers (1)
DGM
on 21 Apr 2021
If you want to preserve a vector, don't use it as the loop iterator.
N=300;
af=linspace(0.4905,49.05,N); % make the vector
for nn=1:numel(af) % index into the vector
[t,theta]=ode45(@(t,theta) pendulumcode2(t,theta,g,d,af(nn),kg),tspan,theta0);
end
% this doesn't do what you think it does
[maxt,maxtheta]=max(theta); % with this syntax, the second output argument is an index
c2=max(theta); % with this syntax, this is a row vector
y2=t(maxtheta);
degree2 = c2(1,1); % this is a scalar
degree = degree2*180/pi % this is a scalar
time = y2(1,1)
hold on
plot(degree,af(:,1)); % you're literally plotting a scalar versus a scalar.
xlabel('theta')
ylabel('af')
hold off
I don't know what you're expecting to get out of the loop, You might want to save the results instead of overwriting them.
1 Comment
Bence Mészáros
on 21 Apr 2021
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!