I am trying to plot kinetic energy as a function of time. I am using fplot inside a foor loop which results in two errors and a warning.
clear; clc
V = 5000;
d = 0.5e-2;
B = 1;
q = 1.6e-19;
m = 1.7e-27;
% T = 2pi/omega
% q V = 0.5 m v^2
% m v^2 = 2 q V
% v = sqrt(2 q V / m)
% q v B = m v^2 / r
% r = 2 V / v B
T(1) = 0;
r(1)= 2*V/(sqrt(2*q*V/m)*B);
n=1;
while r(n)<5e-2
V = n*5000;
r(n+1)= 2*V/(sqrt(2*q*V/m)*B);
T(n+1) = T(n) + (6.67588e-8)/2;
n=n+1;
end
%2*V/(sqrt(2*q*V/m)*B)
n
%plot(T, r, '.-'), legend('Radius')
%xlabel('Time [s]')
%ylabel('Radius [m]')
V=5000;
t=zeros(1,n);
t(1)=1.03077640640442e-08;
for i=1:n
vi=sqrt(2*(i-1)*q*V/m);
vf=sqrt(2*i*q*V/m);
vm=(vf-vi)/2;
t(i+1)=t(i)+d/vm;
ti=t(i);
Ti=T(i);
tf=t(i+1);
Tf=T(i+1);
fplot(@(x) (q.*V./ti).*x,[Ti Ti+tf],'b')
hold on
fplot(@(x) 0.5.*m.*vm.^2,[Ti+tf Tf],'b')
end
hold off
So the errors occurs at the end of the code when I'm trying to plot the functions.
If you have any other suggestions please tell as this code probably looks quite bad.

 Akzeptierte Antwort

Walter Roberson
Walter Roberson am 28 Feb. 2017

0 Stimmen

fplot(@(x) 0.5.*m.*vm.^2,[Ti+tf Tf],'b')
is independent of the input, x. If you want to create a constant with that value, then you need to replicate the constant by the size of the input to produce a vectorized form:
fplot(@(x) 0.5.*m.*vm.^2 .* ones(size(x)),[Ti+tf Tf],'b')
That is what the warning is about.
The error occurs because on i = 2, Ti+tf > Tf so the interval [Ti+tf Tf] becomes backwards.
Since you are plotting a constant there anyhow, you could consider sort() around that vector.

Weitere Antworten (0)

Kategorien

Mehr zu Line Plots finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by