Why do I receive legend error in matlab

close all;
clear all;
clc
m=10;
b=5;
k=100;
F=100;
timerange=[0 20];
initialvalues=[0 0];
[t, x]=ode45(@second_order1,timerange,initialvalues);
figure
plot(t,x(:,1),'LineWidth',2)
ylabel('position(m),velocity(m/s),acceleration(m/s^2)')
xlabel('time(s)')
hold on
plot(t,x(:,2),'LineWidth',2,'LineStyle','-.')
hold on
plot(t,F/m-(b/m)*x(:,2)-(k/m)*x(:,1),'LineWidth',2,'LineStyle',':')
legend ('Position','Velocity','Acceleration')

Réponses (1)

Star Strider
Star Strider le 6 Sep 2020
I cannot run yuour code, since I get:
Unrecognized function or variable 'second_order1'.
What is the error you are seeing?

5 commentaires

Kamar Ledgister
Kamar Ledgister le 6 Sep 2020
legend icons do not display
Kamar Ledgister
Kamar Ledgister le 6 Sep 2020
In the graph
I obviously cannot reproduce that error because I cannot run your code.
When I created my own (arbitrary) version of ‘second_order1’, the Warning thrown is:
Warning: 'Position' interpreted as a legend property name. To include a label with the same name as a legend
property, specify the labels using a cell array or string array.
so I experimented using that information and changed the legend call to:
legend ({'Position','Velocity','Acceleration'})
(note the curly braces {}), and then it worked.
Kamar Ledgister
Kamar Ledgister le 6 Sep 2020
Thank you
Star Strider
Star Strider le 6 Sep 2020
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by