Can someone please help me answer this question, I have no idea what to do.

6 views (last 30 days)
The question I have been given is:
The relationship between voltage, v, and current, i, in an inductor, L, is given by
v = L* (di/dt)
Given that the current, i, is (e^(-0.5*(t-5)^2)) * (sin(10t) + 0.5sin(30t))
Use MATLAB’s diff function to approximate the voltage and hence plot the current and voltage of the inductor for 0<t<10 on the same axes.
You may assume that L = 1.
I have written:
% Since L approx = 1
% v = 1*(di/dt)
t = 0:10;
i = exp(-0.5*(t-5).^2).*(sin(10*t)+(0.5*sin(30*t)));
v = diff(i);
plot(v,t)
hold on
plot(i,t)
hold on
But i get the error
"Error using plot. Vectors must be the same length.
Error in assesment2question2 (line 7) plot(v,t)"
Im not sure why, but the value for v is is not a 1x11 double like t and i, and im not sure why? Sorry if I am being stupid. Any help would be much appreciated.

Accepted Answer

Stephen23
Stephen23 on 9 Dec 2018
Edited: Stephen23 on 9 Dec 2018
Consider what the difference between values actually is, and try some examples:
>> diff([2,5])
ans =
3
>> diff([2,5,9])
ans =
3 4
>> diff([2,5,9,23])
ans =
3 4 14
Notice how the diff output always one element shorter than the input vector.
Some would recommend to simply shorten the time vector by one element.... however this gives a misleading alignment of the two data vectors. A more representative approach is to average adjacent time values:
Ti = 0:0.001:10;
Tv = (Ti(1:end-1)+Ti(2:end))/2;
I = exp(-0.5*(Ti-5).^2).*(sin(10*Ti)+(0.5*sin(30*Ti)));
V = diff(I)./diff(Ti);
plotyy(Ti,I, Tv,V)
legend('I','V')

More Answers (1)

TADA
TADA on 9 Dec 2018
Edited: TADA on 9 Dec 2018
diff Calculates The Difference Between Every Two Adjacent Points In A Vector, Therfore, It's Output Is A Vector With A Length Shorter Than It's Input By One.
To Plot It, use A Shorter t:
plot(t(2:end), v);

Categories

Find more on Line 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!