Based on the following code:
clear vars;
close all;
x1 = [0 0 0.01 0.09 0.1 0.11 0.2 0.3 0.35 0.50 0.64 0.8 1]
y1 = [0.05 0.10 0.15 0.20 0.25 0.30 0.38 0.42 0.45 0.48 0.52 0.86 1]
x2 = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.9 0.9 1]
y2 = [0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 0.9 0.9 1]
plot(x1, y1); hold on;
plot(x2, y2);
I need to calculate the area (green area) between the two curves, for example:
How can I calculate it?
This area is the difference of the two curves integral in the specified domain between each intersection (as mentioned by MBo). Hence, you can find the intersections using InterX and then use trapz to do this:
P = InterX([x1;y1],[x2;y2]);
area = 0;
% for each segment
% each segment is between P(1,i) and P(1, i+1)
% So we can find xsegments with idx = find(x < P(1,i+1) && x > P(1,i)) and [P(1,i) x(idx) P(1,i+1)]
% ...
area = area + abs(trapz(xsegment1i,ysegment1i) - trapz(xsegment2i,ysegment2i));
Since one of the curves is a straight line you can rotate then add up the areas from the new x axis.
The line is at 45 degrees. So the rotation matrix is
cos 45 sin 45
-sin 45 cos 45
Multiply each point in the second curve by that matrix. That gives points with the line as the new x axis. Now use area of the triangle (0.5 * width * height) to add up the areas of the fragments.
Related
I am new to MATLAB and I need help. I have 3 matrices (A, B, and C) and I want to create a new matrix average_ABC that contains average values.
A = [ 0.3 0.5 0.9
0.14 0.36 0.1
0.9 0.5 0.14]
B = [ 0.8 0.9 0.14
0.1 0.25 0.4
0.8 0.14 0.25]
C = [0.25 0.3 0.47
0.12 0.3 0.2
0.14 0.56 0.9]
The resulting matrix will be
average_matrix = [ 0.3 0.5 0.47
0.12 0.25 0.2
0.8 0.5 0.25]
Please, any suggestion, how can I do it?
You can first concatenate your matrices along the third dimension (using cat) and then compute whatever you want using the dim parameter that is available for most functions to specify that you want to perform that operation along the third dimension.
Also you've stated that you want the average (mean), but based on your example you actually want the median. Either way, we can compute them using this method.
data = cat(3, A, B, C);
% Compute the mean
mean(data, 3)
% 0.45 0.56667 0.50333
% 0.12 0.30333 0.23333
% 0.61333 0.4 0.43
% Compute the median (which seems to be what you actually want)
median(data, 3)
% 0.3 0.5 0.47
% 0.12 0.3 0.2
% 0.8 0.5 0.25
I hope this will work
average_matrix=(A+B+C)/3.;
I have a matrix proba (size :10 * 5).
proba=[0.5 0.3 0.8 0.9 0.8;
0.50 0.36 0.58 0.58 0.98;
0.1 0.25 0.6 0.8 0.9;
0.5 0.3 0.8 0.9 0.8;
0.2 0.9 0.58 0.58 0.69;
0.58 0.14 0.1 0.2 0.3;
0.25 0.9 0.8 0.7 0.5;
0.58 0.69 0.25 0.1 0.1;
0.1 0.25 0.36 0.2 0.3;
0.5 0.3 0.8 0.9 0.8 ];
I want to transform this matrix into a text file (proba.txt) with which the index column is written and the value of the column for each line as follows :
1 0.5 2 0.3 3 0.8 4 0.9 5 0.8
1 0.50 2 0.36 3 0.58 4 0.58 5 0.98
.
.
.
1 0.5 2 0.3 3 0.8 4 0.9 5 0.8
Please I need help, how can I do it?thanks in advance
You can use this function, it is useful for every matrix.
function data = addIndex(X)
[r, c] = size(X);
index = ones(r, 1);
data = zeros(r, 2 * c);
for i = 1:c
data(:, 2 * i - 1) = i .* index;
data(:, 2 * i) = X(:, i);
end
dlmwrite('proba.txt', data, '\t')
end
you can easily do this using dlmwrite, but first you want to add the column of indexes in front of your matrix:
function result = writematrix(proba)
rowind = 1:size(proba,2);
for t = 1:size(proba,1);
C(t,:,:) = [rowind',proba(t,:)']';
D(t,:) = C(t(:),:);
end
dlmwrite('filename.txt',D,'\t') %//I assume you want tab delimiter, if you want space, it is ' ' instead
%//dlmwrite('filename.txt',D,' ')
end
Note that this will write the text file into your local directory, and that it only works for numerical values, not strings, for strings, it is better to use csvwrite.
EDIT : Ops, didn't read the question fully, this should now work fine.
I would like to only show these x-values on the x-axes
xx=[0.0005 0.005 0.05 0.1 0.25 0.5 0.75 1 1.25 1.5];
Is it possible?
You can modify axis labels using XtTickLabel property. For example:
set(gca,'XTickLabel',[0.0005 0.005 0.05 0.1 0.25 0.5 0.75 1 1.25 1.5])
This will change only labels, not actual values on the plot. To check values as well you can use:
set(gca,'XTick',[0.0005 0.005 0.05 0.1 0.25 0.5 0.75 1 1.25 1.5]);
set(gca,'XScale','log'); % Your xx values seem to be logarithmic, so this can help.
I have two vectors
A=[0.1 0.1 0.2 0.2 0.3 0.3 0.3 0.3 0.4 0.5 0.5]
B=[0.7 0.7 0.8 0.8 0.9 0.9 0.9 0.9 1 1 1]
How to plot their stem plots and box plots at the same time?
The y-axis should be the probability of the stem plots.
What I am looking for is something like this.
Drawing the box-plots can be accomplished with
boxplot([A B], [ones(size(A)) 2*ones(size(B))], ...
'orientation', 'horizontal', 'positions', [1 1]);
After which you can add the stem plots with
hold on
stem(xa, ya);
stem(xb, yb);
where I'm not sure exactly what you are asking for for x and y.
I'm trying to simulate nonlinear vehicle braking system with ode45.
I take a short time to learning MATLAB. then, I don't know why error occur.
It would be very appreciated if you could point out errors, and tell me how to solve.
code 1. main script
code 2. function
code 3. errors
clear;
global m f Jw rw Fz Cd p A bw fw Nw g uw seta Te Tb T p0
x = [0 0.025 0.05 0.1 0.125 0.15 0.175 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1]
y = [0 0.225 0.45 0.65 0.685 0.705 0.69 0.68 0.65 0.635 0.63 0.6275 0.625 0.6225 0.62 0.6175 0.615 0.6125 0.610 0.6075 0.6050 0.6 0.5975 0.5950]
%plot(x,y)
p0=polyfit(x,y,6)
%y=polyval(p,x)
m = 1400; f = 0.01; Jw = 0.65; rw = 0.31; Fz = 3560.0; Cd = 0.5; p = 1.202; A = 1.95;
bw = 0.0; fw = 0.0; Nw = 4; g = 9.81; uw = 0.0; seta = 0.0; Te = 0.0; Tb = 1000.0; T = Te - Tb;
[t,i] = ode45(#dott,[0.0 1.0],[20 20]);
plot(t,i);
axis([0 1 0 20]);
legend('x1','x2');
function xdot = dott(t,x)
global m f Jw rw Fz Cd p A Nw Te Tb p0
X = [0 0.025 0.05 0.1 0.125 0.15 0.175 0.2 0.25 0.3 0.35 0.4 0.45 0.5 0.55 0.6 0.65 0.7 0.75 0.8 0.85 0.9 0.95 1];
Y = [0 0.225 0.45 0.65 0.685 0.705 0.69 0.68 0.65 0.635 0.63 0.6275 0.625 0.6225 0.62 0.6175 0.615 0.6125 0.610 0.6075 0.6050 0.6 0.5975 0.5950];
%UNTITLED2 Summary of this function goes here
%Detailed explanation goes here
xdot = zeros(2,1);
Y=p0(1,1)*((x(2)-x(1))/x(1))^6+p0(1,2)*((x(2)-x(1))/x(1))^5+p0(1,3)*((x(2)-x(1))/x(1))^4+p0(1,4)*((x(2)-x(1))/x(1))^3+p0(1,5)*((x(2)-x(1))/x(1))^2+p0(1,6)*((x(2)-x(1))/x(1))^1+p0(1,1)*((x(2)-x(1))/x(1));
xdot(1)=(-0.5*p*Cd*A(x(2)/(1+Y)*rw)*(x(2)/(1+Y)*rw)-f*m+Nw*Y*Fz)/(rw*m);
xdot(2)=(Te-Tb-rw*Y*Fz)/Jw;
end
??? Subscript indices must either be real positive integers or logicals.
Error in ==> dott at 9
xdot(1)=(-0.5*p*Cd*A(x(2)/(1+Y)*rw)*(x(2)/(1+Y)*rw)-f*m+Nw*X*Fz)/(rw*m);
Error in ==> odearguments at 98
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ==> ode45 at 172
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in ==> a at 14
[t,i] = ode45(#dott,[0.0 1.0],[20 20]);
I'm pretty sure your error is in this statement:
A(x(2)/(1+Y)*rw)
The way you write it, you're trying to use x(2)/(1+Y)*rw as an index to the scalar A. I guess you want to multiply is this way:
... A * (x(2) / (1 + Y) * rw) ...
To make the code more readable:
Use spaces. A long compact line is really hard to read.
Split long lines into several using three dots ...
Something like this is easier to read in my opinion:
Y = p0(1,1) * ((x(2) - x(1)) / x(1))^6 + p0(1,2) * ...
((x(2) - x(1)) / x(1))^5 + p0(1,3) * ((x(2) - x(1)) / x(1))^4 ...
+ p0(1,4) * ((x(2) - x(1)) / x(1))^3 + p0(1,5) * ((x(2) - x(1)) / ...
x(1))^2 + p0(1,6) * ((x(2) - x(1)) / x(1))^1 + p0(1,1) * ...
((x(2) - x(1)) / x(1));