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

2 visualizzazioni (ultimi 30 giorni)
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 Commento
Robert  Flores
Robert Flores il 18 Set 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')

Accedi per commentare.

Risposta accettata

Rik
Rik il 18 Set 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')

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by