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

2 Ansichten (letzte 30 Tage)
Robert  Flores
Robert Flores am 18 Sep. 2019
Beantwortet: Rik am 18 Sep. 2019
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 Kommentar
Robert  Flores
Robert Flores am 18 Sep. 2019
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')

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Rik
Rik am 18 Sep. 2019
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')

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by