2

Well another problem has pop up recently. I have a set representing a curve and a line I drew with the line() function. So far my code is :

clc, clear all, close all;

n = 800/1500;
I = [ 0 1.1 4 9.5 15.3 19.5 23.1 26 28.2 30.8 33.3 35.9];
E_up = [ 5.8 10.5 28 60.3 85.5 100.3 108 113.2 117 120.5 123.5 126];
E_up = E_up./n;
Iw = [ 34 31.5 28.2 23.9 19.9 16.1 13 8.1 3.5 1.2 0 NaN];
E_down = [124.6 122.5 118.8 112.2 103.9 93.1 81.6 59.1 29.6 14.5 9.5 NaN];
E_down = E_down./n;


x_est = I;
y_est = spline(Iw,E_down,x_est)
A(:,1)= E_up
A(:,2) = y_est

ma = mean(A,2)

% figure()
% hold all
% % plot(x_est,y_est,'ro')
% plot(I,E_up,'b-',Iw,E_down,'g-')
% plot(I,ma,'r')
% grid on
% legend('up','down','mean')

%dane_znamionowe

clc, clear all, close all;

%data_entry
n = 800/1500;
I = [ 0 1.1 4 9.5 15.3 19.5 23.1 26 28.2 30.8 33.3 35.9];
E_up = [ 5.8 10.5 28 60.3 85.5 100.3 108 113.2 117 120.5 123.5 126];
E_up = E_up./n; %rescalling_EMF
Iw = [ 34 31.5 28.2 23.9 19.9 16.1 13 8.1 3.5 1.2 0 NaN];
E_down = [124.6 122.5 118.8 112.2 103.9 93.1 81.6 59.1 29.6 14.5 9.5 NaN];
E_down = E_down./n; %rescalling_EMF
Un = 220;
In = 28.8;
wn = 1500;
wmax = 3000;
P = 5.5e3;
Rs = 15.8/25;

%interpolation
x_est = I;
y_est = spline(Iw,E_down,x_est);

%mean_values
A(:,1)= E_up;
A(:,2) = y_est;
ma = mean(A,2);

%party_Xd
figure()
[ax,h1,h2] = plotyy(I+30,wn,I,ma,'plot','plot');
set(ax(1),'ylim',[0 3000],'ytick',[1500  3000]);
set(ax(2),'ylim',[0 300],'ytick',[100 200 300]);
hold(ax(1))
hold(ax(2))

%stable_parts
set(ax,'NextPlot','add')
plot(ax(2),I,ma,'b')
plot(ax(2),0,Un,'m*')
i2 = 0:0.01:70;
plot(ax(2),i2,Un-(i2*Rs),'m--')
iin = 0:1:300;
plot(ax(2),In,iin,'g-')
plot(ax(1),i2,wn,'k-','linewidth',8)
plot(ax(1),28.8,1500,'g*')

%loop
p1x = [35 45 55 65];

for ii = 1 :length(p1x)
    x11 = p1x(ii);
    y11 = 0;
    x21 = In;
    y21 = wn;
    x1 = [35 45 55 65];
    y1 = [0 0 0 0];
    x2 = [In In In In];
    y2 = [wn wn wn wn];
    slope = (y21-y11)/(x21-x11);
    xLeft = 0; 
    yLeft = slope * (xLeft - x11) + y11;
    xRight = 70; 
    yRight = slope * (xRight - x11) + y11;
    plot(ax(2),x11,0,'r.')
    a1 = line([xLeft, xRight], [yLeft, yRight], 'Color', 'c');

    x0 = (max(min(x1),min(x2))+min(max(x1),max(x2)))/2;
    fun1 = @(x) interp1(x1,y1,x,'linear');
    fun2 = @(x) interp1(x2,y2,x,'linear');
    difffun = @(x) fun1(x)-fun2(x);
    crossing = fzero(difffun,x0); %crossing x coordinate
    crossval = fun1(crossing);
end

My graph looks like this which is pretty decent.But I need to find the intersection point of the cyan line and blue curve.

enter image description here

4
  • 2
    Please post only the relevant code, no one wants to read the code for the three other lines.
    – Daniel
    Nov 29, 2015 at 20:15
  • I suggest checking out my answer to another question, which didn't solve that problem, but it would solve yours, I believe:) Nov 29, 2015 at 20:29
  • @AndrasDeak the problem in your crossing output is that it gives out only the x coordinate whereas i need both x,y. I would be more than glad if you could craft my code to that point. Thank you anyway. Nov 29, 2015 at 20:42
  • You just have to substitute into either of the interpolating functions. I added an answer anyway. Nov 29, 2015 at 21:04

1 Answer 1

1

An answer based on my solution to a similar question:

%dummy input
x1=[0 1 2 3]; 
y1=[1 4 2 0];
x2=[-1 3 4 5];
y2=[-1 2 5 3];

x0 = (max(min(x1),min(x2))+min(max(x1),max(x2)))/2;
fun1 = @(x) interp1(x1,y1,x,'linear','extrap');
fun2 = @(x) interp1(x2,y2,x,'linear','extrap');
difffun = @(x) fun1(x)-fun2(x);

crossing = fzero(difffun,x0); %crossing x coordinate
crossval = fun1(crossing); %substitute either function at crossing point

plot(x1,y1,'b-',x2,y2,'r-',crossing,crossval,'ks');
legend('line1','line2','crossover','location','nw');

after which your crossing point is given by [crossing, crossval].

Result:

result

23
  • I have added the part of code that you posted and i get an error that Interpolation requires at least two sample points in each dimension. Nov 29, 2015 at 21:17
  • p1x = [35 45 55 65]; for ii = 1 :length(p1x) x1 = p1x(ii); y1 = 0; x2 = In; y2 = wn; slope = (y2-y1)/(x2-x1); xLeft = 0; yLeft = slope * (xLeft - x1) + y1; xRight = 70; yRight = slope * (xRight - x1) + y1; plot(ax(2),x1,0,'r.') a1 = line([xLeft, xRight], [yLeft, yRight], 'Color', 'c'); x0 = (max(min(x1),min(x2))+min(max(x1),max(x2)))/2; fun1 = @(x) interp1(x1,y1,x,'linear'); fun2 = @(x) interp1(x2,y2,x,'linear'); difffun = @(x) fun1(x)-fun2(x); crossing = fzero(difffun,x0); crossval = fun1(crossing); end Nov 29, 2015 at 21:17
  • @sayidjetzenden You need to define vectors x1, y1, x2, y2. In your comment you have a single value for each. But you don't have to redefine those: just use the same variables which you use for plotting those lines...... Nov 29, 2015 at 21:20
  • ` x1 = [35 45 55 65]; y1 = [0 0 0 0]; x2 = [In In In In]; y2 = [wn wn wn wn];` I tried this and i get the same error. Nov 29, 2015 at 21:27
  • 1
    @sayidjetzenden please forget the habit of using code from answers without attribution. I suggest adding an "update" part to your question, preferably with a note saying that you're also using my solution. But in the end, Stack Overflow prefers if answers are only in actual answers, so there is no need to edit the question to contain a valid answer. That said, I know we haven't solved your problem yet. Nov 29, 2015 at 21:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.