How to I turn a fprintf column of values into a matrix
1 view (last 30 days)
Show older comments
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
0 Comments
Answers (1)
James Tursa
on 1 Dec 2020
Edited: James Tursa
on 1 Dec 2020
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.
0 Comments
See Also
Categories
Find more on Annotations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!