How to find the point the graph crosses the x axis

25 views (last 30 days)
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')

Accepted Answer

Matt J
Matt J on 19 Nov 2024
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')

More Answers (1)

Walter Roberson
Walter Roberson on 18 Nov 2024
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

Categories

Find more on Graph and Network Algorithms 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!