How to increment a subscript in Matlab using the ylabel function?

1 次查看(过去 30 天)
Hello,
I am trying to make my subscripts in my for loop interate with each loop. Below is a copy of my code. An example of how I would like the code to work is Figure(1) having the labels of x, for the x-axis, and PSI_1(x) for the y-axis. If anyone can help me in resolving this issue, it'll be greatly appreciated.
Very Respectfully,
Robert
CODE:
clc, clear, close all
L = 100; a = 0; b = L; N = L;
% Making a string of texts for Graph Titles
s = ["First","Second","Third","Fourth"];
for n=1:4 % 1st Four Energy States
syms x
psi = @(x) sqrt(2/L)*sin(n*pi*x/L);
ic = eval(subs(diff(psi,x,1),0)); % Inital Conditions
alpha = [0 ic];
[w, t] = rk4_system(@(t,ps) analogous_pendulum(t,ps,n,L),a,b,N,alpha);
figure
hold on
fplot(psi,[0 100],'r','LineWidth',2) % Plot Analytical Solution
plot(t,w(1,:),'o b') % Plot of RK4
title(sprintf('%s Stationary State of the Infinite Square Well', s(n)))
ylabel('\psi_'+n+'(x)'), xlabel('x'),legend('Analytical', 'RK4')
end
  1 个评论
Robert  Flores
Robert Flores 2019-9-18
If this helps I am getting the following error.
Error using +
Matrix dimensions must agree.
Error in ME_500_HMWK3 (line 120)
ylabel('\psi_'+n+'(x)'), xlabel('x'),legend('Analytical', 'RK4')

请先登录,再进行评论。

采纳的回答

Rik
Rik 2019-9-18
The syntax you are attempting to use would work if you're using strings. However, you are using char arrays. Either switch to a string syntax, or use sprintf to create the annotation.
%option 1:
ylabel("\psi_"+n+"(x)"), xlabel('x'),legend('Analytical', 'RK4')
%option 2:
%double slash to escape the slash
ylabel(sprintf('\\psi_%d(x)',n)), xlabel('x'),legend('Analytical', 'RK4')

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by