对于练习,我必须创建函数 x^i 从 i=1 到 i=10 的动画图(以 Octave 为单位)。该图必须有一个动态标题,并且曲线的颜色必须改变。目前我的代码如下:
function exercicequatorze
x=linspace(0,1,100);
for i=1:10
plot(x,x.^i,'color',[i/10,0,1-i/10])
a=num2str(i);
title(["Le graphe de la fonction x^",a] )
pause(1)
endfor
endfunction
它的表现几乎符合预期,我什至惊讶地发现我的标题中出现了 x1、x2、x3、...,而不是 x^1、x^2 等。然而,在最后一张图中,我的标题以 x10 结尾,而不是 x1⁰。关于如何解决这个问题有什么想法吗?我尝试摆弄大括号,但要么出现语法错误,要么应该是我的指数的数字被转移到标题中的第二行。
最佳答案
您需要将数字放在大括号中。
title(["Le graphe de la fonction x^{" a "}"])
documentation
中有解释:
If the
'^'
or'_'
is followed by a{
character, then all of the block surrounded by the{ }
pair is superscripted or subscripted. Without the{ }
pair, only the character immediately following the'^'
or'_'
is changed.
关于octave - 作为字符串中指数的变量( Octave ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66526602/