0

I have 2 different sets of values corresponding to a magnetization curve. My problem is that I cannot actually compute the mean values of the curves. I am thinking I should use some kind of interpolation, but I don't know how.

My code so far:

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];
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];
n = 800/1500;

plot(I,E_up,Iw,E_down)
grid on 
legend ('up', 'down')

%loop for mean values

for ii = 1:length(I)
    E1(ii) = ((E_down(13-ii)));
    E2(ii) = E_up(ii);
    E4(ii) = mean([E1(ii),E2(ii)]);
    I2(ii) = Iw(13-ii)
    I3(ii) = mean([I2(ii),I(ii)])
end


hold on     
plot(I3,E4,'r-')
plot(I3(7),E2(7),'co',I3(7),E1(7),'ro')
plot(I3,E1,'c-',I3,E2,'g-')

Here is the graph I am getting and its totally wrong

enter image description here

*edit The problem is I am actually getting the average curve of cyan and light green instead of blue and dark green.

5
  • What is wrong with the plot? Please explain your expected output, especially what the mean of a curve is in this context.
    – Daniel
    Nov 29, 2015 at 14:40
  • Its supposed to be a curve with average values of the two curves id. E_up and E_down Nov 29, 2015 at 14:47
  • I can see 5 curves, considering you stored them as a column-matrix A, your "mean curve" is simply mean(A,2). For the two case, get mean(A(:,[1 5]),2)
    – Adriaan
    Nov 29, 2015 at 14:47
  • @Adriaan can u explain a bit more? I did like A = [E_up' E_down'] to get a 24x1 matrix of the values but it doesnt seem to be working the mean(A(:,[1 5]),2). Nov 29, 2015 at 14:53
  • @Daniel please check the edit in original post. Nov 29, 2015 at 15:00

2 Answers 2

1
%// Trump up some curves
tmp = [1:1e3].';%'//
A(:,1) = tmp;
A(:,2) = 2.*tmp;
A(:,3) = 0.5*tmp+1;
A(:,4) = 2.2.*(tmp+0.2);
A(:,5) = 1.3.*tmp;

%// calculate means
B = mean(A,2);
C = mean(A(:,[1 5]),2);

figure;
hold on
plot(A,'b')
plot(B,'r') %// mean of all blue curves
plot(C,'g') %// mean of the top and bottom curves

mean works on matrices and can be set to take the mean per row setting the second input to 2.

enter image description here

2
  • Well, I tried: clc, clear all, close all; 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]; 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]; n = 800/1500; E_down2 = fliplr(E_down) A(:,1)=E_up' A(:,2) = E_down2' ma = mean(A,2) figure() plot(I,ma,'r') hold on plot(I,E_up,'b-',Iw,E_down,'g-') and i got imgur.com/8zUoYKG What is wrong as well. Nov 29, 2015 at 15:11
  • I'm thinking that the main problem is that i don't have the same values on I and Iw and i must somehow interpolate the one curve to find the corresponding E_up or E_down values. Nov 29, 2015 at 15:28
0

Well i have interpolated the one curve and thereafter used mean. So my code is:

clc, clear all, close all;

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];
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];
n = 800/1500;

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(I,E_up,'b-',Iw,E_down,'g-')
plot(I,ma,'r')
grid on
legend('up','down','mean')
1
  • In your question you simply ask for a way to calculate the average of two curves. I provided you with an answer to that, which apparently works, since you use my code, albeit without citation, to answer your own question. From the question itself it's not clear you need to interpolate, you should edit that in. As well it's customary under the SO referencing to credit your sources.
    – Adriaan
    Nov 29, 2015 at 21:26

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.