Plotting a vertical line using xline with a value

조회 수: 7 (최근 30일)
rezheen
rezheen 2025년 6월 10일
편집: Adam Danz 2025년 6월 10일
Hello, I'm trying to plot a vertical dashed line with a value (set as a variable) I have found with my code, but it won't plot.
When I change the variable in xline to the actual number (5.5), it plots the dashed line. This is the code and the problem is very last line but it depends on previous lines. Thanks.
clear, clc
syms t k C1 C
d2s=-k; dsdt=88; t1=0; s1=0;
d2=int(d2s,t); % Find the antiderivative of acceleration wrt. t.
d2_t1=subs(d2,t1);
eqn1=dsdt==d2_t1+C1; C1=solve(eqn1,C1); % Solve for C1
dsdt=d2+C1; % Velocity function
s_=int(dsdt); s=expand(s_);
s_t1=subs(s,t1);
eqn1=s1==s_t1+C; C=solve(eqn1,C); % Solve for C
s=s+C; % Position function
% % Solve the dsdt eqn above (set = 0) for t.
dsdt1=0; eqn=dsdt1==dsdt; t_val=solve(eqn,t);
% % Solve for k with s = 242 and t=t_val
s2=242; eqn1=s2==s;
eqn=subs(eqn1, t, t_val);
k_val=solve(eqn,k);
new_s=subs(s,k,k_val); new_v=subs(dsdt,k,k_val);
new_t_val=subs(t_val,k,k_val);
v2=subs(new_v,t,new_t_val);
f=fplot(new_s); set(f,'color','b','Linewidth', 1.2);hold on;grid on;
g=fplot(new_v); set(g,'color','r','LineStyle', '--');hold on;
j=xline(0); set(j,'color','black','Linewidth', 1.5);
k=yline(0); set(k,'color','black','Linewidth', 1.5);
plot(t1,s1,'.', 'color', 'b','markersize',15);hold on;
plot(new_t_val,s2,'.', 'color', 'b','markersize',15);hold on;
plot(new_t_val,v2,'.', 'color', 'r','markersize',15);hold on;
l=xline(new_t_val); set(l,'color','r','LineStyle', '--');
Error using xline (line 35)
Value must be one of the following: 'numeric' | 'datetime' | 'categorical' | 'duration'

채택된 답변

Adam Danz
Adam Danz 2025년 6월 10일
편집: Adam Danz 2025년 6월 10일
new_t_val is symbolic. As the error message indicates, xline expects to recieve numeric, datetime, categorical, or duration values.
Convert the symbolic value to double.
l=xline(double(new_t_val));

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by