Plotting a vertical line using xline with a value

3 vues (au cours des 30 derniers jours)
rezheen
rezheen le 10 Juin 2025
Modifié(e) : Adam Danz le 10 Juin 2025
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'

Réponse acceptée

Adam Danz
Adam Danz le 10 Juin 2025
Modifié(e) : Adam Danz le 10 Juin 2025
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));

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by