Plot two lines on loglog
Show older comments
I am using the function tic-toc to time how long it takes for my code to run. I want to be able to plot the iteration my code runs from 10^1-10^8, by using loglog. On the x-axis I want the iterations my code is ran for, and on the y-axis i want time it took for the code to run in each iteration. I'm pretty much trying to compare the time it takes for 2 equations, to run, that give me the same answer. I tried plotting it using loglog, but I can't seem to make it work. My professor told me to define time1 as a vector like I did for rtime1, but I don't really understand. Can someone steer me in the right direction please?
function Homework2
clc
a = [1,0,3];
b = [2,-2,-1];
c = 10;
fun1 = @(LJ1) term1(LJ1); %Calls function METHOD1, it will be timed by the following code
t1 = 1; %Iteration counter
tic;
for n = c^1:c^1:c^8
rtime1(t1) = n; %counts by the times 'n' runs (ITERATIONS)
stime1(t1) = tic;
t1 = t1+1; %each time code is run, counter increases by 1 for time
end
time1 = toc
fun2 = @(LJ2) term2(LJ2); %Calls function METHOD2
t2 = 1;
tic;
for n = c^1:c^1:c^8
rtime2(t2) = n; %counts by the times 'n' runs
stime2(t2) = tic;
t2 = t2+2;
end
time2 = toc
%loglog(rtime1,stime1)
plot(rtime2,stime2)
%loglog(rtime1,stime1,rtime2,stime2)
end
%Two separate POINTER functions are made for calculations
function y = term1(LJ1) %METHOD1
r = sqrt(sum((a-b).^2)); %Vectors are subtracted, then squared, then added together, and finally rooted
LJ1 = (1/r^6)+(1/r^12)
end
function y = term2(LJ2) %METHOD2
r2 = (sum((a-b).^2));
alpha = 1/(r2);
beta = alpha^3; %[1/r2^6] = (1/r2)(1/r2)(1/r2)
gamma = beta^2; %[1/r2^12] = (1/r2^6)(1/r2^6)
LJ2 = beta+gamma
end
1 Comment
Robert Calderon
on 12 Sep 2020
Answers (0)
Categories
Find more on Scatter Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!