How to find the point the graph crosses the x axis

29 次查看(过去 30 天)
Luis
Luis 2024-11-18,20:06
回答: Matt J 2024-11-19,0:13
Hello,
So I am trying to find the value when it crosses the x axis for the first time on the dammped spring graph each and label it so that I can a specific value. Any help would be very much appreciated
t = 0:0.005:5;
x = (0.3) .* exp(-3 .*t) .* cos(4 .* t) + 0.2 .*exp(-3 .* t) .* sin(4 .* t);
%% graphs part
plot(t,x)
hold on
yline(0,'LineStyle','--','Color','m')
xline(0.5397,'LineStyle','--','Color','r')
grid on
xlabel('Time/s')
ylabel('Displacement/m')

回答(2 个)

Walter Roberson
Walter Roberson 2024-11-18,20:15
if x(1) == 0
crossing_location = 1;
elseif x(1) < 0
%crossing from negative to positive
crossing_location = find(x >= 0, 1, 'first');
else
%crossing from positive to negative
crossing_location = find(x <= 0, 1, 'first');
end
if isempty(crossing_location)
%no crossings!
else
t_crossing = t(crossing_location);
x_crossing = x(crossing_location);
end

Matt J
Matt J 2024-11-19,0:13
x = @(t) (0.3) .* exp(-3 .*t) .* cos(4 .* t) + 0.2 .*exp(-3 .* t) .* sin(4 .* t);
firstroot = fzero(x,[0,1])
%% graphs part
fplot(x,[0,5])
hold on
yline(0,'LineStyle','--','Color','m')
xline(firstroot,'LineStyle','--','Color','r')
grid on
xlabel('Time/s')
ylabel('Displacement/m')

类别

Help CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by