Is that the correct way to plot the graph ?

조회 수: 1 (최근 30일)
Netanel Malihi
Netanel Malihi 2021년 12월 21일
답변: Star Strider 2021년 12월 21일
I have two functions and i would like to plot a graph.
My concern is that when i call Ja function it wont take the correct t because, it might think that when i call Ja it outputs only one output x and not t. the second function works the same so i will post only one.
n1 = 10:1:5000; %Range vector of n for Jacobi function.
n2 = 10:100:5010; %Range vector of n for GaussPartialPivoting2 function.
ts1 = zeros(size(n1)); %Vector of the time for Jacobi function.
ts2 = zeros(size(n2)); %Vector of the time for GaussPartialPivoting2 function.
for i = 1:numel(n1)
[x,ts1(i)]=Ja(n1(i));
end
for j=1:numel(n2)
[x,ts2(j)]=GaussPartialPivoting2(n2(j));
end
%plot graph
subplot(2,1,1)
plot(n1,ts1,'LineWidth',1);
title('Jacobi')
xlabel('number of equations')
ylabel('time for solutions [sec]')
subplot(2,1,2)
plot(n2,ts2,'r','LineWidth',1)
title('Gauss Partial Pivoting')
xlabel('number of equations')
ylabel('time for solutions [sec]')
function [x,t]=Ja(n)
tic
v4=repelem(4,n); %Creats a vector with value of 4 n(input) times.
v1=repelem(-1,n-1); %Creats a vector with value of -1, n-1 times(input-1) times.
A=diag(v4)+diag(v1,-1)+diag(v1,1); %Creats a matrix with v4 vector as diagonal and v1 as as diagonal under and on top the v4 diagonal.
A(1,n)=-1; %assigns value -1 to the (1:n) position in the matrix.
A(n,1)=-1; %assigns value -1 to the (n:1) position in the matrix.
b=1:n; % creats b a row vector of solutions.
B=b'/n; % Changing b from row vector to a column vector.
x = zeros(n,1); %Initial guess.
epsilon = 1e-6; %required accurecy/Maximum error.
flag = 0;
counter = 0; %Set initial iteration counter.
%Disassemble marix A
L = tril(A,-1); %Builds lower triangular of matrix A without the diagonal.
D = diag(A); %Builds only the diagonal of matrix A.
U = triu(A,1); %Builds upper triangular of matrix A without the diagonal.
BB = -1./D.*(L+U); %Build new Matrix BB from the decomposition of matrix A.
c = (1./D).*B;
while flag == 0
counter = counter + 1;
if counter > 10000
error('Too many iterations');
end
x_new = BB*x+c; %Updating solution until the stop condition is met.
if max(abs(x_new-x)./abs(x)) < epsilon %stop condition
flag = 1;
end
x = x_new; %Final Solution
end
t=toc;
sprintf('Time elapsed : %g',t);
end

채택된 답변

Star Strider
Star Strider 2021년 12월 21일
It plots ‘t’ correctly and plots it —
plot(n1,ts1,'LineWidth',1);
however the sprintf is not going to produce anything because no output is assigned to it and the terminal semicloln (;) will suppress the output —
sprintf('Time elapsed : %g',t);
The semicolon will suppress output to the Command Window, so removing it will allow sprintf to pprint to the Command Window —
sprintf('Time elapsed : %g',t)
.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by