How to convert fprintf from single row to multiple rows?

 Réponse acceptée

Star Strider
Star Strider le 25 Juin 2022
There is not enough of the code provided to know what the problem is (and images of code are never appropriate, since actual code that can be copied, pasted, and run is always preferable).
That code should be writing the results 1000 times, so whatever is causing it not to do so is likely ‘over the horizon’ and out of sight here.

5 commentaires

% Secant Method in MATLAB
a=input('Enter function:','s');
f=inline(a)
x0 = input ('Enter the first initial guess: ')
x1 = input ('Enter the second initial guess: ')
Ea = input ('Enter absolute error: ')
iteration=0;
disp('Iteration x0 x1 x2 f(x0) f(x1) f(x2) Ea')
for i=1:1000
x2 = x0 - ((f(x0))*((x1-x0)/(f(x1)-f(x0))));
Error = round(abs(x2-x1),4);
fprintf(['%i %11.5f %11.5f %11.5f %11.5f %11.5f %11.5f %11.5f %11.5f\n'],i,x0,x1,x2,f(x0),f(x1),f(x2),Error)
iteration=iteration+1;
if (Error<=Ea)
Root=x2
Error=Ea
Iteration=iteration
break
end
x0=x1;
x1=x2;
end
Hi Sir! I wanted it to like this...(please see attached image)
I am not certain what the selected input parameters are, however the problem is now obvious.
The calculated value of ‘Error’ is less than the value of ‘Ea’ in the first iteration so the if condition is satisfied (true) and the loop stops. I am not certain how best to correct that, other than to select a much lower value for ‘Ea’ than is currently entered so that the condition is not satisfied on the first iteration.
EDIT — (25 Jun 2022 at 14:54)
There are 9 edit descriptors in the fprintf statement with only 8 inputs. Matching those solved the problem.
With those inputs, and that correction, this runs correctly for me —
% Secant Method in MATLAB
% a=input('Enter function:','s');
% f=inline(a)
a = 'x^3 - 4*x^2 + x - 10'
a = 'x^3 - 4*x^2 + x - 10'
f = str2func(['@(x)' a])
f = function_handle with value:
@(x)x^3-4*x^2+x-10
% x0 = input ('Enter the first initial guess: ')
% x1 = input ('Enter the second initial guess: ')
% Ea = input ('Enter absolute error: ')
x0 = 3;
x1 = 4;
Ea = 0.0001;
iteration=0;
disp('Iteration x0 x1 x2 f(x0) f(x1) f(x2) Ea')
Iteration x0 x1 x2 f(x0) f(x1) f(x2) Ea
for i=1:1000
x2 = x0 - ((f(x0))*((x1-x0)/(f(x1)-f(x0))));
Error = round(abs(x2-x1),4);
fprintf(['%i %11.5f %11.5f %11.5f %11.5f %11.5f %11.5f %11.5f\n'],i,x0,x1,x2,f(x0),f(x1),f(x2),Error)
iteration=iteration+1;
if (Error<=Ea)
Root=x2
Error=Ea;
Iteration=iteration
break
end
x0=x1;
x1=x2;
end
1 3.00000 4.00000 4.60000 -16.00000 -6.00000 7.29600 0.60000 2 4.00000 4.60000 4.27076 -6.00000 7.29600 -0.79078 0.32920 3 4.60000 4.27076 4.30295 7.29600 -0.79078 -0.08773 0.03220 4 4.27076 4.30295 4.30697 -0.79078 -0.08773 0.00129 0.00400 5 4.30295 4.30697 4.30691 -0.08773 0.00129 -0.00000 0.00010
Root = 4.3069
Iteration = 5
NOTE — The inline function has been (or soon will be) depricated, so use str2func (with or without the vectorize function) instead.
The input function does not work with the online Run feature that I use here.
.
Hi! I appreiciate the information you have shared with me, and I appreciate your prompt reply.
As always, my pleasure!
I found it somewhat surprising that the fprintf statement was the problem. I learned something from this.

Connectez-vous pour commenter.

Plus de réponses (1)

I assume you want to show each numeric value in a new line. To do that, add a new line control character where you want to insert a new line,
fprintf('%11.5f\n%11.5f\n%11.5f\n%11.5f\n%11.5f\n', rand(1,6))
0.50328 0.90133 0.47589 0.81838 0.27507 0.93488

6 commentaires

I'll try this one sir. Thank you
Hi sir! I wanted it to like like this.. (please see attached image)
% Secant Method in MATLAB
a=input('Enter function:','s');
f=inline(a)
x0 = input ('Enter the first initial guess: ')
x1 = input ('Enter the second initial guess: ')
Ea = input ('Enter absolute error: ')
iteration=0;
disp('Iteration x0 x1 x2 f(x0) f(x1) f(x2) Ea')
for i=1:1000
x2 = x0 - ((f(x0))*((x1-x0)/(f(x1)-f(x0))));
Error = round(abs(x2-x1),4);
fprintf('%i %11.5f %11.5f %11.5f %11.5f %11.5f %11.5f %11.5f %11.5f\n',i,x0,x1,x2,f(x0),f(x1),f(x2),Error)
iteration=iteration+1;
if (Error<=Ea)
Root=x2
Error=Ea
Iteration=iteration
break
end
x0=x1;
x1=x2;
end
Please provide the inputs the user should enter.
Also, please describe again what the problem is because it's still not entirely clear.
Enter function:
x^3 - 4*x^2 + x - 10
Enter the first initial guess:
3
Enter the second initial guess:
4
Enter absolute error:
0.0001
the problem are the answers after 5 iterations they appeared in a single row. what i wanted is like a multiple rows.....
1 x0 x1 answer answer Ea
2 x0 x1 answer answer Ea
3 x0 x1 answer answer Ea
4 x0 x1 answer answer Ea.
5 x0 x1 answer answer Ea

Connectez-vous pour commenter.

Catégories

En savoir plus sur Scripts dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by