How to I turn a fprintf column of values into a matrix

3 ビュー (過去 30 日間)
reece pankhania
reece pankhania 2020 年 12 月 1 日
編集済み: James Tursa 2020 年 12 月 1 日
I have a list of values in a column from my 'fprintf' code:
fprintf ('x \t\t y (Euler)\t y (analytical) \n')
fprintf ('%f \t %f\t %f\n',x0,y,f(x0))
for x = x0 : h : xn-h
y = y + dy(x,y)*h
x = x + h
fprintf ('%f \t %f\t %f\n',x,y,f(x))
end
This gives me a considerable number of values in a fprintf code. I however want to plot my two 'Y' values against eachother of a graph against the x value

回答 (1 件)

James Tursa
James Tursa 2020 年 12 月 1 日
編集済み: James Tursa 2020 年 12 月 1 日
You can save all the values for plotting. E.g.,
xp = x0;
yp = y;
fp = f(x0);
k = 1;
for x = x0 : h : xn-h
y = y + dy(x,y)*h
x = x + h
fprintf ('%f \t %f\t %f\n',x,y,f(x))
k = k + 1;
xp(k) = x;
yp(k) = y;
fp(k) = f(x);
end
Then you can plot xp, yp, fp.
If you have a large number of values to plot, you should probably preallocate the plotting variables. E.g.,
n = numel(x0 : h : xn-h);
xp = zeros(n,1);
yp = zeros(n,1);
fp = zeros(n,1);
xp(1) = x0;
yp(1) = y;
fp(1) = f(x0);
etc.

カテゴリ

Help Center および File ExchangeAnnotations についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by