How do I add a legend to my plot?
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
%Define the variables using syms - syms creates symbolic variables
syms y(t) y0
%define the ordinary differential equation
ode = diff(y,t) == cos(t^2);
%solve the differential equation using dsolve
ysol = dsolve(ode,y(0)==y0);
%Create a figure called Task 1
figure ('Name','Task 1')
%Pick 3 different initial conditions for which the solution exists
%Conditions are the y values (y values are 1, 1.5, and 3 at varying t
%values. See the t values below.
conds = [1 1.5 3]'; %some values of y0
f = matlabFunction(subs(ysol,y0,conds));
t = linspace(0,50);
y = f(t);
%plotting the 3 equations
plot(t,y,'linewidth',2)
title('Symbolic Solutions')
xlabel('t')
ylabel('y(t)')
grid on
채택된 답변
Star Strider
2019년 7월 31일
To add the legend, either use sprintfc (undocumented although quite useful):
lgndc = sprintfc('IC = %.1f',conds);
legend(lgndc, 'Location','E')
lgndc = compose('IC = %.1f',conds);
legend(lgndc, 'Location','E')
both produce the same result.
Put these lines after your ‘grid on’ call.
댓글 수: 14
Vincenzo Dragone
2019년 7월 31일
For some reason this isn't working in my code...I replaced the IC with my initial condition y(0)...
What does ‘not working’ mean?
I copied your code exactly and added only those two lines. (I am running R2019a.)
%Define the variables using syms - syms creates symbolic variables
syms y(t) y0
%define the ordinary differential equation
ode = diff(y,t) == cos(t^2);
%solve the differential equation using dsolve
ysol = dsolve(ode,y(0)==y0);
%Create a figure called Task 1
figure ('Name','Task 1')
%Pick 3 different initial conditions for which the solution exists
%Conditions are the y values (y values are 1, 1.5, and 3 at varying t
%values. See the t values below.
conds = [1 1.5 3]'; %some values of y0
f = matlabFunction(subs(ysol,y0,conds));
t = linspace(0,50);
y = f(t);
%plotting the 3 equations
plot(t,y,'linewidth',2)
title('Symbolic Solutions')
xlabel('t')
ylabel('y(t)')
grid on
lgndc = sprintfc('IC = %.1f',conds);
legend(lgndc, 'Location','E')
Vincenzo Dragone
2019년 7월 31일
I am running R2018b which is why I think it's a problem.
It keeps giving me the following error.
Unable to use a value of type 'cell' as an index.
Error in Untitled3 (line 23)
legend(lgndc, 'Location','E')
Running R2018b is not the problem.
In your Command Window, type:
which('legend','-all')
and post all the results in a Comment here.
The only results should be something like:
C:\Program Files\MATLAB\R2019a\toolbox\matlab\scribe\legend.p
C:\Program Files\MATLAB\R2019a\toolbox\matlab\scribe\legend.m % Shadowed
I suspect you will have more outputs than that, and the extra outputs should direct you to finding the problem.
Vincenzo Dragone
2019년 7월 31일
I downloaded the R2019a version to my computer and got it to work. Thank you!
Star Strider
2019년 7월 31일
My pleasure.
If my Answer helped you solve your problem, please Accept it!
Vincenzo Dragone
2019년 7월 31일
I'm trying to accept, but I don't see where the button is???
Star Strider
2019년 7월 31일
It should be something like this:

Thank you!
Rena Berman
2019년 7월 31일
편집: Rena Berman
2019년 7월 31일
(Answers dev) Vincenzo, I have accepted the answer for you. Were you logged in when you couldn't see the button? What commands do you have available to you under Star Strider's picture (which is just to the left of his answer?) For example:

If you are logged in, you should be able to see an accept/unaccept option right under the vote/unvote command. This is in addition to the "Accept this answer" button that Star Strider mentions above.
Vincenzo Dragone
2019년 7월 31일
Hi Rena,
Yes, I am logged into my account. But the accept/unaccept option was not popping up. I saw it last night, forgot to hit it, but then this morning I couldn't find it.
The commands under Star Strider's picture are Vote, Flag, and Link. That's all.
Rena Berman
2019년 7월 31일
(Answers dev) Thanks for the quick reply! I'll look in to it.
Star Strider
2019년 7월 31일
Thank you both!
Vincenzo Dragone
2019년 7월 31일
Star Strider,
Would you be able to help me with another question?
Using Euler's Method, I need to approximate the solution to the IVP over some t range which I get to choose and then plot it.
My initial condition is y(0)=1 like in Task 1 code.
Here is all the code I have so far.
%% Task 1: Solve the ODE using the Symbolic Math Package and dsolve()
%Define the variables using syms - syms creates symbolic variables
syms y(t) y0 y1 h ode y2
%define the ordinary differential equation
ode = diff(y,t) == cos(t^2);
%solve the differential equation using dsolve
ysol = dsolve(ode,y(0)==y0);
%Create a figure called Task 1
figure ('Name','Task 1')
%Pick 3 different initial conditions for which the solution exists
%Conditions are the y values (y values are 1, 1.5, and 3 at varying t
%values. See the t values below.
conds = [1 1.5 2]'; %some values of y0
f = matlabFunction(subs(ysol,y0,conds));
t = linspace(0,5);
y = f(t);
%plotting the 3 equations
plot(t,y,'linewidth',2)
title('Symbolic Solutions')
xlabel('t')
ylabel('y(t)')
grid on
lgndc = sprintfc('IC = %.1f',conds);
legend(lgndc, 'Location','E')
%% Task 2: Euler's Method
%Pick a single initial condition from Task 1
%The initial condition we will use is y(0)=1
%Use Euler's method to approximate the solution to this IVP over some t
%range which you choose
%Create a figure called Task 2
figure ('Name','Task 2')
%The range of t is from 0 to 1
%We are using the initial condition that y(0)=1, but we want to know y(t)
%when t=1
%In other words we want to find y(1)
y = y0;
yout = y;
t0 = 0;
h = 0.5;
tfinal = 50;
for t= t0 : h : tfinal-h
y = y + h*ode;
yout = [yout;y]
end
Star Strider
2019년 7월 31일
Jim Riggs already appears to have answered that to your satisfaction: How do you use Euler's Method to approximate the solution?
I doubt that I could improve on his Answer:.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Symbolic Math Toolbox에 대해 자세히 알아보기
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
