problem with fprintf command

3 次查看(过去 30 天)
i don't get the all values of the respective variable column wise.
Code:
close all
clear all
y = @(x) x.^3-x-11;
x = -10:1:10;
f = y(x);
fprintf('%8s %8s\n','x','f')
x f
fprintf('%8.2f %8.2f\n',x',f')
-10.00 -9.00 -8.00 -7.00 -6.00 -5.00 -4.00 -3.00 -2.00 -1.00 0.00 1.00 2.00 3.00 4.00 5.00 6.00 7.00 8.00 9.00 10.00 -1001.00 -731.00 -515.00 -347.00 -221.00 -131.00 -71.00 -35.00 -17.00 -11.00 -11.00 -11.00 -5.00 13.00 49.00 109.00 199.00 325.00 493.00 709.00 979.00
what is wrong i can't find out. can anyone please help me.
Thanks in advance.
  1 个评论
VBBV
VBBV 2023-12-5
close all
clear all
y = @(x) x.^3-x-11;
x = -10:1:10;
f = y(x);
fprintf('%8s %8s\n','x','f')
x f
fprintf('%8.2f %8.2f\n',[x;f])
-10.00 -1001.00 -9.00 -731.00 -8.00 -515.00 -7.00 -347.00 -6.00 -221.00 -5.00 -131.00 -4.00 -71.00 -3.00 -35.00 -2.00 -17.00 -1.00 -11.00 0.00 -11.00 1.00 -11.00 2.00 -5.00 3.00 13.00 4.00 49.00 5.00 109.00 6.00 199.00 7.00 325.00 8.00 493.00 9.00 709.00 10.00 979.00

请先登录,再进行评论。

采纳的回答

Walter Roberson
Walter Roberson 2023-12-5
close all
clear all
y = @(x) x.^3-x-11;
x = -10:1:10;
f = y(x);
fprintf('%8s %8s\n','x','f')
x f
disp(char(compose("%8.2f %8.2f",x',f')))
-10.00 -1001.00 -9.00 -731.00 -8.00 -515.00 -7.00 -347.00 -6.00 -221.00 -5.00 -131.00 -4.00 -71.00 -3.00 -35.00 -2.00 -17.00 -1.00 -11.00 0.00 -11.00 1.00 -11.00 2.00 -5.00 3.00 13.00 4.00 49.00 5.00 109.00 6.00 199.00 7.00 325.00 8.00 493.00 9.00 709.00 10.00 979.00
  2 个评论
Walter Roberson
Walter Roberson 2023-12-5
When you use fprintf(), all of the first data parameter is converted (in linear order) before any of the second data parameter is converted. The form of fprintf() you used will not produce column outputs.
There is a way, though:
close all
clear all
y = @(x) x.^3-x-11;
x = -10:1:10;
f = y(x);
fprintf('%8s %8s\n','x','f')
x f
fprintf('%8.2f %8.2f\n',[x; f])
-10.00 -1001.00 -9.00 -731.00 -8.00 -515.00 -7.00 -347.00 -6.00 -221.00 -5.00 -131.00 -4.00 -71.00 -3.00 -35.00 -2.00 -17.00 -1.00 -11.00 0.00 -11.00 1.00 -11.00 2.00 -5.00 3.00 13.00 4.00 49.00 5.00 109.00 6.00 199.00 7.00 325.00 8.00 493.00 9.00 709.00 10.00 979.00
This creates a 2D array with two rows, the first row of which is a value from x, and the second row of which is the corresponding value from f. And then "linear order" of the resulting array alternates elements of x and f, so columns can be output.
It takes a while to get accustomed to.
When you use compose(), then compose() knows to go across the data parameters using one value from each parameter, so compose() produces columns of output easily.
Walter Roberson
Walter Roberson 2023-12-5
It might be easier to use compose() for output in this form
close all
clear all
y = @(x) x.^3-x-11;
x = -10:1:10;
f = y(x);
fprintf('%8s %8s\n','x','f')
x f
fprintf("%s\n", compose("%8.2f %8.2f",x',f'));
-10.00 -1001.00 -9.00 -731.00 -8.00 -515.00 -7.00 -347.00 -6.00 -221.00 -5.00 -131.00 -4.00 -71.00 -3.00 -35.00 -2.00 -17.00 -1.00 -11.00 0.00 -11.00 1.00 -11.00 2.00 -5.00 3.00 13.00 4.00 49.00 5.00 109.00 6.00 199.00 7.00 325.00 8.00 493.00 9.00 709.00 10.00 979.00

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 String 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by