函数参数包括h(= 10)。我希望情节标题包含相同的h。怎么做?
function G=graphit(X,Y,ye,h)
plot(X,Y,'-');
grid
title([ 'Approximate and Exact Solution @h= .', num2str(h)])
谢谢。 MM
您可以使用sprintf
创建格式化字符串
title( sprintf( 'Approximate and Exact Solution. h = %.0f', h ) );
title(['Approximate and Exact Solution ',num2str(h),' .'])
title_string = sprintf('Approximate and Exact Solution @h= %d.',h) % change d to f for floats
title(title_string)
我使用正确的字符串格式化工具,如sprintf
来构建格式正确的标题。
尽管在不到5分钟的时间内给出了3个优秀的答案,但没有一个建议的代码可以正常运行。基本上,我得到了与运行原始代码时几乎相同的结果。
事实证明,对于h的数字中的前导零(例如01或05)将导致系统降低零。这对我来说是一个问题,因为我希望h值为.05,.025,.01。此外,Matlab软件似乎与指定的小数点混淆,后跟带有前导零的数字。解决这个问题的方法是用小数点(.10,.05,.025,。01)传递小数点。见下面的代码。
输入是
X,Y,xe,ye,.01
工作代码:
function G=graphit(X,Y,xe,ye,h)
hold on;
plot(X,Y,'-'); plot(X,ye,'-.');
hold off
title([ 'Approximate and Exact Solution @h=', num2str(h)])
预期和获得的输出:近似和精确解@ h = 0.01
瞧!谢谢你的回复......