Borrar filtros
Borrar filtros

I would like to display my results in a table containing the number of iterations of the for loop and their solution

3 visualizaciones (últimos 30 días)
clear
clc
disp('Welcome to calculation program')
n=input('Please enter the number of iterations=');
x=input('Please enter the value of vector x=');
for i=1:1:n
x(i+1)=x(i)-((2*x(i)*tan(x(i))-1)/(2*tan(x(i))+2*x(i)*(sec(x(i))^2)));
fprintf('The value of iteration no.%1.0f of vector x is=%2.4f\n',i,x(i+1))
plot(i,x,'*b')
hold on
xlabel('Number of iterations')
ylabel('x values')
grid on
if x(i)==x(i+1)
break
end
end
for i=1:1:n
if x(i)~=x(i+1)
disp('The maximum number of iterations have been reached')
y=input('To re-run press 1 and to stop press any number=');
if y==1
midexam3
elseif y~=1
break
end
end
end

Respuestas (1)

Naman Kaushik
Naman Kaushik el 19 de Jun. de 2023
Hi Reem,
As per my understanding, you want to tabulate your results for different iterations.
You could try using the uitable function in MATLAB. It needs two parameters, namely “Data” and “Position, which is specified as a four-element vector of the form [left bottom width height].
You can find the updated code below:
clear;
clc ;
disp('Welcome to calculation program')
n=input('Please enter the number of iterations=');
Cell_Array = cell(n ,2);
Headers = {"Iterations", "Results"};
x=input('Please enter the value of vector x=');
for i=1:1:n
x(i+1)=x(i)-((2*x(i)*tan(x(i))-1)/(2*tan(x(i))+2*x(i)*(sec(x(i))^2)));
Cell_Array{i, 1} = i;
Cell_Array{i, 2} = x(i+1);
fprintf('The value of iteration no.%1.0f of vector x is=%2.4f\n',i,x(i+1))
plot(i,x,'*b')
hold on
xlabel('Number of iterations')
ylabel('x values')
grid on
if x(i)==x(i+1)
break
end
end
hold off;
% Display the table
figure;
uitable('Position', [20 20 260 160], 'Data', Cell_Array, 'ColumnName', Headers);
For more information on the uitable function, you can refer to the following documentation:

Categorías

Más información sobre Numeric Types en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by