0

I need to find the current of a diode in Matlab there are 2 equations and I couldn't find an intersection point. There is no real intersection point in this functions but I need to find the closest possible current values possible (need 3 correct digits after point) current code is here;

clc;
close all;
clear all;
a=27;% tempature in celcius
b=2*(10.^(-14));%saturation current
q=1.6e-19;%electron charge
k=1.38e-23;%boltzman's constant
t=a+273;%temp in kelvin
v=-0.2:0.00001:0.715;%source voltage
i=b*(exp(q*v/(k*t))-1);%diode i-v characteristic formula
i2=(5-v)/1000;%kirchoff's voltage law formula
plot (v,i,v,i2)    
xlabel('Voltage -->')
ylabel('Current -->')
grid on;
axis([0.2 2 0 0.03])`

I need to find closest i and i2 values. And because of I'm going to do it repeatedly I have to formulate it.

Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
  • Welcome to Stack Overflow! Since you're new here, I recommend reading ["How do I ask a good question?"](https://stackoverflow.com/help/how-to-ask) for some tips. Your question is lacking sufficient detail to help the community help you. – Marc LaFleur Jan 27 '18 at 16:26
  • [This should help.](https://stackoverflow.com/questions/20331097/synchronize-intersection-points-of-two-pairs-of-curves-with-fminsearch) – Robert Seifert Jan 27 '18 at 16:57

2 Answers2

1

This solution makes us of the min function, which determines the minimum of a vector as well as the index where this minimum occurs:

[difference,index] = min(abs(i-i2));
disp(v(index));   % point where both curves are closest
disp(difference); % how close they are at that point
flawr
  • 10,814
  • 3
  • 41
  • 71
1

Instead of evaluating your i and i2 curves numerically by directly applying the computations to the given range v, you could define two distinct function handles as follows:

i_fun = @(v) b .* (exp((q .* v) / (k * t)) - 1);
i2_fun = @(v) (5 - v) / 1000;

Once this is done, you can compute the curve values as follows:

v = -0.2:0.00001:0.715;
i = i_fun(v);
i2 = i2_fun(v);

This will allow you to use the fsolve function to detect the intersections much more easily:

diff_fun = @(v) i2_fun(v) - i_fun(v);
int_x = fzero(diff_fun,3);
int_y = feval(i_fun,int_x);

Full working example:

clc;
close all;
clear all;

a = 27;
b = 2*(10.^(-14));
q = 1.6e-19;
k = 1.38e-23;
t = a + 273;

i_fun = @(v) b .* (exp((q .* v) / (k * t)) - 1);
i2_fun = @(v) (5 - v) / 1000;
diff_fun = @(v) i2_fun(v) - i_fun(v);

v = -0.2:0.00001:0.715;%source voltage
i = i_fun(v);
i2 = i2_fun(v);
int_x = fzero(diff_fun,3);
int_y = feval(i_fun,int_x);

plot(v,i,v,i2);
hold on;
plot(int_x,int_y,'ob');
hold off;

Output:

Output

Tommaso Belluzzo
  • 23,232
  • 8
  • 74
  • 98
  • 1
    This is probably the best solution if the curves are indeed crossing, but there should be a final verification to find where their distance is minimal in case they don't. – Benjamin Barrois Jan 27 '18 at 18:21