Invalid File Identifier?

4 visualizaciones (últimos 30 días)
Noah Reilly
Noah Reilly el 14 de Feb. de 2021
Editada: Walter Roberson el 15 de Feb. de 2021
The following code is giving this error:
Error using fprintf
Invalid file identifier. Use fopen to generate a valid file identifier.
fprintf(fileID, 'Newton Raphson Method \n')
%Open File for Writing
fileID = fopen ('Newton-Raphson Method', 'w');
syms x % Sets X as a symbolic variable
f = x.^3-x.^2+2.*x-2.4; % Function
f_prime = diff(f); %Derivative of function
error_tolerance = 0.001; %Tolarance
x_nod = 0.6; %Initial Guess for Root
%N = Iteration Number
%I = Guess Value
%Error = Error
fprintf(fileID, 'Newton Raphson Method \n')
fprintf(fileID, '%-6s%-14.3s%-8.4s\n','n','I','Error') %Builds Table in CLC
for n = 1:50 %Iteration Number
f_nod = vpa(subs(f,x,x_nod)); %Calculates value at x_nod
f_nod_der = vpa(subs(f_prime,x,x_nod)); %Calculate the Derivative at x_nod
y = x_nod-f_nod/f_nod_der; %Newton-Raphson formula
error = abs(y-x_nod); % Error formula
fprintf(fileID, '%-6d%-14.3f%-8.4f%%\n',n,y,error)
if error < error_tolerance % if Error does not meet tolerance established the function restarts
break
end
x_nod = y; %If Error DOES meet tolerance then x_nod is set equal to y and sent down
end
fprintf (fileID, '\nThe root is: %0.3f \n', y); %Displays Output
fprintf (fileID, 'The Error is: %0.4e \n', error);
fprintf (fileID, 'Number of Iterations: %d \n', n);
I've tried everything I can think of, what's wrong?

Respuestas (1)

Walter Roberson
Walter Roberson el 15 de Feb. de 2021
Editada: Walter Roberson el 15 de Feb. de 2021
[fileID, msg] = fopen ('Newton-Raphson Method', 'w');
if fid<0
error('cannot open file because: "%s"', msg)
end

Productos


Versión

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by