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

1 回表示 (過去 30 日間)
Robert  Flores
Robert Flores 2019 年 9 月 18 日
回答済み: Rik 2019 年 9 月 18 日
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 Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by