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

조회 수: 5 (최근 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 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