I would like to display my results in a table containing the number of iterations of the for loop and their solution
1 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Naman Kaushik
2023-6-19
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:
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Whos 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!