how to find intersection points between 2 curves - matlab

I have been trying to find intersection between 2 curves, my formulas are long, and I do not know what how to find the value at x axis
Here is my code:
Dr = (0:0.001:7);
a1 = (2*Dr + t)/t;
b1 = (h - 1.45*Dr)/t;
c1 = (h - k4*Dr)/t;
d1 = a1.*b1;
e1 = d1.*c1;
Fs1 = Dr.*((4000*E/(1 - v^2))*(t^3 / Do^2)*(k2/k1)*(1 + 0.153*e1));
Fs1 = double(Fs1);
a2 = ((Dr-dss)*ks - 0.1*t)/t;
b2 = (h - 2.2*(Dr-dss)*ks)/t;
c2 = (h - K4*(Dr-dss)*ks)/t;
d2 = a2.*b2;
e2 = d2.*c2;
f = (Dr -dss)*ks;
Fb2 = f.*((250*E/(1 - v^2))*(t^3 / (k1*Do^2))*(1 + 0.133*e2));
n = ks*Fb2;
n = double(n);
n(Dr<3.47)=0;
plot (Dr,n,Dr,Fs1)
ylim([0 600]);
grid on;

Related

artificial neural network in octave

I'm having trouble on an easy exercise about an artificial neural network with 2 features, a hidden layer of 5 neurons and two possible outputs (0 or 1).
My X matrix is a 51x2 matrix, and y is a 51x1 vector.
I know I'm not supposed to do the while E>1 but I wanted to see if eventually my error would be lower than 1
I'd like to know what I am doing wrong. My error doesn't seem to lower (around 1.5 no matter how much iterations I'm doing). Do you see in the code where I am doing a mistake? I'm supposed to use gradient descent.
function [E, v,w] = costFunction(X, y,alpha1,alpha2)
[m n] = size(X);
E = 1;
v = 2*rand(5,3)-1;
w = 2*rand(2,6)-1;
grad_v=zeros(size(v));
grad_w=zeros(size(w));
K = 2;
E = 2;
while E> 1
a1 = [ones(m,1) X];
z2 = a1 * v';
a2 = sigmoid(z2);
a2 = [ones(size(a2,1),1),a2];
z3 = a2 * w';
h = sigmoid(z3);
cost = sum((-y.*log(h)) - ((1-y).*log(1-h)),2);
E = (1/m)*sum(cost);
Delta1=0;
Delta2=0;
for t = 1:m
a1 = [1;X(t,:)'];
z2 = v * a1;
a2 = sigmoid(z2);
a2 = [1;a2];
z3 = w * a2;
a3 = sigmoid(z3);
d3 = a3 - y(t,:)';
d2 = (w(:,2:end)'*d3).*sigmoidGradient(z2);
Delta2 += (d3*a2');
Delta1 += (d2*a1');
end
grad_v = (1/m) * Delta1;
grad_w = (1/m) * Delta2;
v -= alpha1 * grad_v;
w -= alpha2 * grad_w;
end
end

"Solve" command with a vector in matlab

I have a problem with the equation shown below. I want to enter a vector in t2 and find the roots of the equation from different values in t2.
t2=[10:10:100]
syms x
p = x^3 + 3*x - t2;
R = solve(p,x)
R1 = vpa(R)
Easy! Don't use syms and use the general formula:
t2 = [10:10:100];
%p = x^3 + 3*x - t2;
a = 1;
b = 0;
c = 3;
d = -t2;
D0 = b*b - 3*a*c;
D1 = 2*b^3 - 9*a*b*c + 27*a^2*d;
C = ((D1+sqrt(D1.^2 - 4*D0.^3))/2).^(1/3);
C1 = C*1;
C2 = C*(-1-sqrt(3)*1i)/2;
C3 = C*(-1+sqrt(3)*1i)/2;
f = -1/(3*a);
x1 = f*(b + C1 + D0./C1);
x2 = f*(b + C2 + D0./C2);
x3 = f*(b + C3 + D0./C3);
Since b = 0, you can simplify this a bit:
% ... polynomial is the same
D0 = -3*a*c;
D1 = 27*a^2*d;
% ... the different C's are the same
f = -1/(3*a);
x1 = f*(C1 + D0./C1);
x2 = f*(C2 + D0./C2);
x3 = f*(C3 + D0./C3);
Trial>> syms x p
Trial>> EQUS = p == x^3 + 3*x - t2
It is unknown that you want to solve an equation or a system.
Suppose that you want to solve a system.
Trial>> solx = solve(Eqns,x)
But, I do not think you can find roots.
You can solve one equation.
Trial>> solx = solve(Eqns(3),x)
As far as I know, Maple can do this batter.
In general, loops should be avoided, but this is the only solution that hit my brain right now.
t2=[10:10:100];
pp=repmat([1,0,3,0],[10,1]);
pp(:,4)=-t2;
for i=1:10
R(i,:) =roots(pp(i,:))';
end

MATLAB Linear Mapping of Complex Function

In the z plane (z = x + j*y), I have y = 2x + 4.
I would like to map in the plane under the mapping w = 2*z +6.
My script is as follows:
syms x y real
z = x + i*y;
w = 2*z + 6;
u = real(w)
% u = 2*x + 6
v = imag(w)
%v = 2*y
My Problem is how to insert or substitute u = 2*x + 6 and v = 2*y to the equation y = 2*x + 4 in MATLAB and solve it for v.
So the answer should be v = 2*u - 4.
Finally, I can solve the problem with the following script
clc; clear all;
syms x y u v real
z = x + i*y;
w = 2*z + 6;
f = real(w) - u % f = 2*x - u + 6
x = solve(f, x) % x = u/2 - 3
f = imag(w) - v % f = 2*y - v
y = solve(f, y)% y = v/2
f = subs(y-2*x-4,{x},{u/2-3}) % f = v/2 - u + 2
v = solve(f, v) % v = 2*u - 4

Interpolating data between two known functions matlab

I have two lines y1 = -a1*x1 + c1 for theta =30 and y1 = -a2*x1 + c2 for theta = 45
is it possible to interpolate a equation for y1 for theta between 30 and 45 in matlab ? The lines are almost parallel to each other. Anyone has an easy way of doing this?
You can interpolate the coeff a and c:
w = (theta - 30) / (45 - 30 ); % w = 0 for theta = 30 and w = 1 for theta = 45
aTheta = a2 * w + a1 * ( 1 - w );
cTheat = c2 * w + c1 * ( 1 - w );
yTheta = -aTheta * x + cTheta * y;
x = 1:10;
a30 = 1;
a45 = 1.1;
c30 = 0;
c45 = 3;
y30 = -a1*x + c1;
y45 = -a2*x + c2;
Now to find y40 we can just interpolate the curve parameters (i.e. slope (a) and offset (c))
a40 = interp1([30,45], [a30, a45], 40);
c40 = interp1([30,45], [c30, c45], 40);
And now our interpolated y40 is
y40 = -a40*x + c40;
plot(x,y30,x,y45,x,y40);

Matlab - Multiplying a list of matrices

I am attempting to create a Abeles matrix formalism model to analyse some experimental data - I have attached a wiki link to this for reference so that you can see what I an attempting to achieve.
The crux of my issue is that I am unable to multiply four sets of matrices against each other, as in: A[1]*B[1]*C[1]*D[1], A[2]*B[2]*C[2]*D[2], ..., A[n]*B[n]*C[n]*D[n]. I then need to store the results as individual matrices of their own - each matrix represents the corresponding momentum transfer value from Qmin:Qstep:Qmax.
Also, when I attempt to carry out the final step; R = abs((ABCD(2,1)./ABCD(1,1)).^2) I end up with a single value rather than a value of R for each Q value.
Due to the size of the code a simple for loop isn't a realistic option.
my 'test' code is:
%import data fid = fopen('run_22208_09.dat');
%A = textscan(fid,'%f%f%f',270,'headerlines',0,'delimiter',',');
NQ = size(A{1,1});
NQ = NQ(1);
Qmin = A{1,1}(1);
Qmax = A{1,1}(NQ);
Qstep = A{1,1}(2) - A{1,1}(1);
fclose('all');
s0 = 2e-6;
s1 = 10e-6;
s2 = 6e-6;
s3 = 4e-6;
s4 = 8e-6;
sn = 12e-6;
r1 = 2;
r2 = 10;
r3 = 3;
r4 = 7;
t1 = 10;
t2 = 45;
t3 = 5;
t4 = 20;
Q=Qmin:Qstep:Qmax;
k = 2.*Q;
k1 = (((k).^2) - 4.*pi.*(s1 - s0)).^0.5;
k2 = (((k).^2) - 4.*pi.*(s2 - s0)).^0.5;
k3 = (((k).^2) - 4.*pi.*(s3 - s0)).^0.5;
k4 = (((k).^2) - 4.*pi.*(s4 - s0)).^0.5;
kn = (((k).^2) - 4.*pi.*(sn - s0)).^0.5;
layer1 = ((k1 - k2)./(k1 + k2)).*(exp(-2.*k1.*k2.*(r1.^2)));
beta1 = (sqrt(-1)).*k1.*t1;
layer2 = ((k2 - k3)./(k2 + k3)).*(exp(-2.*k2.*k3.*(r2.^2)));
beta2 = (sqrt(-1)).*k2.*t2;
layer3 = ((k3 - k4)./(k3 + k4)).*(exp(-2.*k3.*k4.*(r3.^2)));
beta3 = (sqrt(-1)).*k3.*t3;
layer4 = ((k4 - kn)./(k4 + kn)).*(exp(-2.*k4.*kn.*(r4.^2)));
beta4 = (sqrt(-1)).*k4.*t4;
%general matrix
C1 = [exp(beta1),layer1.*(exp(beta1));layer1.*exp(-beta1),exp(-beta1)]
C2 = [exp(beta2),layer2.*(exp(beta2));layer2.*exp(-beta2),exp(-beta2)];
C3 = [exp(beta3),layer3.*(exp(beta3));layer3.*exp(-beta3),exp(-beta3)];
C4 = [exp(beta4),layer4.*(exp(beta4));layer4.*exp(-beta4),exp(-beta4)];
% CA = bsxfun(#times,C1,C2)
% CB = bsxfun(#times,CA,C3);
% C = bsxfun(#times,CB,C4)
% R = abs((C(2,1)./C(1,1)).^2)
For element-wise multiplications of arrays, you write
M = C1 .* C2 .* C3 .* C4;