I have a csv file with uniform points but i would like to write MATLAB code for obtain the derivative of the plotted function(unknown) with time. How can i do this?
if true
% code
end

 채택된 답변

Star Strider
Star Strider 2018년 9월 28일

0 개 추천

It is easiest to use the gradient (link) function:
[D,S,R] = xlsread('Uniform_points.csv');
t = D(:,1);
f = D(:,2);
dt = mean(diff(t));
df = gradient(f,dt);
figure
subplot(2,1,1)
plot(t, f)
grid
title(S{2},'Interpreter','none')
xlabel(S{1})
subplot(2,1,2)
plot(t, df)
grid
title(['d(',S{2},')/dt'],'Interpreter','none')
xlabel(S{1})

댓글 수: 6

jasper kansiime
jasper kansiime 2018년 9월 29일
What would the code be if the points in the csv file were NON UNIFORM? what would change?
The work-around I use for that (using the notation in my Answer) is:
dfdt = gradient(f)./gradient(t);
It seems to work well enough, since I’ve successfully used the same approach in other situations.
jasper kansiime
jasper kansiime 2018년 9월 29일
okay looks good as well. I would just like to understand what " ./ " means in the line " * dfdt = gradient(f)./gradient(t);* "
Star Strider
Star Strider 2018년 9월 29일
That creates element-wise division. The ‘.’ in front of multiplication, division, and exponentiation operators makes them element-wise operators. (The exception is the transpose operator ('), where the ‘.’ converts it from a complex-conjugate transpose to an ‘ordinary’ transpose.)
See: Array vs. Matrix Operations (link) for a full discussion.
jasper kansiime
jasper kansiime 2018년 9월 30일
Thanks for the discussion link.It clarified things alot.I have another question
In the above code you have" plot(t, f)" where "t" represents time axis. How can i scale the current time axis(0-1second) into an X-axis from(0- 0.4seconds) in steps of 0.04 seconds?
To simply alter the plot x-tick labels, this works:
t_new = 0:0.04:0.4;
figure
plot(t, f)
xt = get(gca, 'XTick');
xtv = linspace(min(xt), max(xt), numel(t_new));
set(gca, 'XTick', xtv, 'XTickLabel',t_new)
I generalised it, so it is a bit more complicated than it needs to be for the current values. It will work for other ‘t_new’ vectors without further modification.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

태그

질문:

2018년 9월 28일

댓글:

2018년 9월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by