How do I edit a loop to be able to store the variables and outputs used in each loop in a way which can be graphed?
Mostra commenti meno recenti
I have an assignment where I need to collect multiple outputs from a loop utilizing in order to create a graph with all of the outputs. I am struggling with setting up the loop to change a variable (R) every loop while storing the previous outputs in a way which can be used with the function and loop we were given. I have to do this using two different methods that use different matlab functions and different loops structures, but my issue applies to both.
I attempted to define R as an array including all of the different values I needed outputs for, but it returned errors at the function when I tried to run it due to the dimensions not agreeing.
D = 0.3
k = 0.0002
R = 10^4:10^5:10^8
% Define the function and its derivative
f = @(x) (1/sqrt(x))+(2*log((k/(3.7*D))+(2.51/(R*sqrt(x))))) ; % Function f(x)
df = @(x) -((1000 * R * k + 18574 * D) * sqrt(x) + 9287 * D) / (2000 * R * k * x^2 + 18574 * D * x^(3 / 2)); % First derivative f'(x)
% Newton's Method Parameters
x0 = 0.01; % Initial guess
tol = 1e-4; % Tolerance
max_iter = 100; % Maximum iterations
fprintf('\n===== Newton’s Method with Convergence Rate =====\n');
fprintf('%5s %12s %12s %12s %12s %12s\n', ...
'Iter', 'x_n', 'f(x_n)', 'Error', 'Conv. Rate');
iter = 0; % Iteration counter
error = Inf; % Initial large error
prev_error = NaN; % Previous error for convergence rate
while error > tol
iter = iter + 1;
% Compute next approximation
x_new = x0 - f(x0) / df(x0);
% Compute error
error = abs(x_new - x0);
% Compute convergence rate
if iter > 2
conv_rate = log(error / prev_error) / log(prev_error / prev_prev_error);
else
conv_rate = NaN; % Not enough data to compute rate
end
% Print iteration details
fprintf('%5d %12.6f %12.6f %12.6f %12.6f\n', ...
iter, x0, f(x0), error, conv_rate);
% Update values for next iteration
prev_prev_error = prev_error;
prev_error = error;
x0 = x_new;
% Stopping condition
if abs(error) < tol
break;
end
end
% Display final result
fprintf('\nApproximate Root: %.6f\n', x0);
fprintf('Total Iterations: %d\n', iter);
The function being used involved multiple loops/iterations to reach a final result, so I am not sure where or how I can repeatedly store the outputs (in this case, x_new or x0 I think) without affecting any part of the code in the next iteration. The code runs properly and gives an output when R is not an array. I am looking to use values for R between 10^4 and 10^8.
I am very inexperienced with Matlab so I am very sorry if I used any incorrect terms, if my question is confusing, or if I included too much of my code. Thank you all in advance.
Risposta accettata
Più risposte (1)
D = 0.3
k = 0.0002
Rvalues = 10^4:10^5:10^8;
nR=numel(Rvalues);
X=nan(1,nR);
for i=1:nR
R=Rvalues(i);
% Define the function and its derivative
f = @(x) (1/sqrt(x))+(2*log((k/(3.7*D))+(2.51/(R*sqrt(x))))) ; % Function f(x)
df = @(x) -((1000 * R * k + 18574 * D) * sqrt(x) + 9287 * D) / (2000 * R * k * x^2 + 18574 * D * x^(3 / 2)); % First derivative f'(x)
X(i)=doNewton(x0,f,df);
end
function x0=doNewton(f,df)
% Newton's Method Parameters
x0 = 0.01; % Initial guess
tol = 1e-4; % Tolerance
max_iter = 100; % Maximum iterations
fprintf('\n===== Newton’s Method with Convergence Rate =====\n');
fprintf('%5s %12s %12s %12s %12s %12s\n', ...
'Iter', 'x_n', 'f(x_n)', 'Error', 'Conv. Rate');
iter = 0; % Iteration counter
error = Inf; % Initial large error
prev_error = NaN; % Previous error for convergence rate
while error > tol
iter = iter + 1;
% Compute next approximation
x_new = x0 - f(x0) / df(x0);
% Compute error
error = abs(x_new - x0);
% Compute convergence rate
if iter > 2
conv_rate = log(error / prev_error) / log(prev_error / prev_prev_error);
else
conv_rate = NaN; % Not enough data to compute rate
end
% Print iteration details
fprintf('%5d %12.6f %12.6f %12.6f %12.6f\n', ...
iter, x0, f(x0), error, conv_rate);
% Update values for next iteration
prev_prev_error = prev_error;
prev_error = error;
x0 = x_new;
% Stopping condition
if abs(error) < tol
break;
end
end
% Display final result
fprintf('\nApproximate Root: %.6f\n', x0);
fprintf('Total Iterations: %d\n', iter);
end
Categorie
Scopri di più su Programming in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
