Related
I have an equation: y=ax^3 + bx^2 + cx + d and the list of values x = 1, 2, 3, 4 when y = 3, 4, 3, -6 respectively. In Octave, I want to:
(a) Set up a system of four equations involving a, b, c and d. For example, substituting (x, y) = (1,3) into the polynomial gives the equation 3 = a + b + c + d.
(b) Solve the system in (a).
I've been trying to find how to do this for three hours and found nothing. Any help would be appreciated
Thanks.
pstscrpt - I have to do everything in Octave, even though I could find it by hand
Written without any ; at end of assignements so you can see what is going on.
You problem is basically a linear system in the variables [a,b,c,d]'=z
So you need to build a system A*z=y, where A is a matrix 4x4, y and z are column vector size 4
x=[1,2,3,4]'
y=[3,4,3,-6]'
A=zeros(4,4)
for i=1:4
A(i,:)= [ x(i)^3, x(i)^2, x(i), 1]
endfor
z=A\y
the outcome will be
z =
-1.00000
5.00000
-7.00000
6.00000
In Matlab: start by just substituting the different values of x and y you wrote in the expression a*x^3 + b*x^2 + c*x + d = y as:
syms a b c d
eqn1 = a*1^3 + b*1^2 + c*1^1 +d == 3 ;
eqn2 = a*2^3 + b*2^2 + c*2^1 +d == 4 ;
eqn3 = a*3^3 + b*3^2 + c*3^1 +d == 3 ;
eqn4 = a*4^3 + b*4^2 + c*4^1 +d == -6 ;
Then Use equationsToMatrix to convert the equations into the form AX = B. The second input to equationsToMatrix specifies the independent variables in the equations.:
[A,B] = equationsToMatrix([eqn1, eqn2, eqn3, eqn4], [a, b, c,d ])
and the solution for a,b,c,d is:
X = linsolve(A,B)
you can also use if you want
sol = solve([eqn1, eqn2, eqn3, eqn4], [a, b, c,d ])
I'm writing a program to optimize a packing problem using Robert Lang's algorithm from his webpage: https://langorigami.com/wp-content/uploads/2015/09/ODS1e_Algorithms.pdf
This is used to design origami models and it's a problem from circle packing with constraints and different radius.
I have created an example with 5 nodes, where 4 of them are terminal.
Looking at these definitions included in the previous algorithm, I want to work with the Scale Optimization (A.3).
According to the notation, in my example I have:
E = {e1, e2, e3, e4,} ---> Edges
U = {u1, u2. u3, u4, u5} ---> Vertices (Each one with x and y coordinates)
Ut = {u2, u3, u4, u5} ---> Terminal Vertices
P = {[u1, u2], [u1, u3], [u1, u4], [u1, u5],
[u2, u3], [u2, u4], [u2, u5],
[u3, u4], [u4, u5]} ---> All paths between vertices from U
Pt = {[u2, u3], [u2, u4], [u2, u5],
[u3, u4], [u3, u5],
[u4, u5]} ---> All paths between terminal vertices Ut
And if I assume that σ is equal to 0:
L = {100, 50, 100, 100,
150, 200, 200,
150, 150,
200} --> Length of the paths P
Note that the length of each path is in the same position of P.
So now, with all this defined, I have to optimize the scale.
For that, I should:
Minimize (-m) over {m,ui ∈ Ut} s.t.: (A-2)
1.- 0 ≤ ui,x ≤ w for all ui ∈Ut (A-3)
2.- 0 ≤ ui,y ≤ h for all ui ∈Ut (A-4)
3.- (A-5) from webpage I provided.
So, in the example, merging (A-3) and (A-4) we can set the Lb and Ub for the fmincon function such as:
Lb = zeros(1,8)
Ub = [ones(1,4)*w, ones(1,4)*h]
The input x for the fmincon function, due to refusing matrix, I'm using it like:
X = [x1 x2 x3 x4 y1 y2 y3 y4], that's why Lb and Ub has that form.
About the (A-5), we get this set of inequalities:
m*150 - sqrt((x(2)-x(3))^2 + (y(2)-y(3))^2) <= 0
m*200 - sqrt((x(2)-x(4))^2 + (y(2)-y(4))^2) <= 0
m*200 - sqrt((x(2)-x(5))^2 + (y(2)-y(5))^2) <= 0
m*150 - sqrt((x(3)-x(4))^2 + (y(3)-y(4))^2) <= 0
m*150 - sqrt((x(3)-x(5))^2 + (y(3)-y(5))^2) <= 0
m*200 - sqrt((x(4)-x(5))^2 + (y(4)-y(5))^2) <= 0
My main program is testFmincon.m:
[x,fval,exitflag] = Solver(zeros(1,10),zeros(1,10),ones(1,10)*700);
%% w = h = 700
I implemented the optimizer in the file Solver.m:
function [x,fval,exitflag,output,lambda,grad,hessian] = Solver(x0, lb, ub)
%% This is an auto generated MATLAB file from Optimization Tool.
%% Start with the default options
options = optimoptions('fmincon');
%% Modify options setting
options = optimoptions(options,'Display', 'off');
options = optimoptions(options,'Algorithm', 'sqp');
[x,fval,exitflag,output,lambda,grad,hessian] = ...
fmincon(#Objs,x0,[],[],[],[],lb,ub,#Objs2,options);
Where Objs2.m is:
function [c, ceq] = Objs2(xy)
length = size(xy);
length = length(2);
x = xy(1,1:length/2);
y = xy(1,(length/2)+1:length);
c(1) = sqrt((x(2) - x(3))^2 + (y(2)-y(3))^2) - m*150;
c(2) = sqrt((x(2) - x(4))^2 + (y(2)-y(4))^2) - m*200;
c(3) = sqrt((x(2) - x(5))^2 + (y(2)-y(5))^2) - m*200;
c(4) = sqrt((x(3) - x(4))^2 + (y(3)-y(4))^2) - m*150;
c(5) = sqrt((x(3) - x(5))^2 + (y(3)-y(5))^2) - m*150;
c(6) = sqrt((x(4) - x(5))^2 + (y(4)-y(5))^2) - m*200;
ceq=[x(1) y(1)]; %% x1 and y1 are 0.
end
And Objs.m file is:
function c = Objs(xy)
length = size(xy);
length = length(2);
x = xy(1,1:length/2);
y = xy(1,(length/2)+1:length);
c(1) = sqrt((x(2) - x(3))^2 + (y(2)-y(3))^2) - m*150;
c(2) = sqrt((x(2) - x(4))^2 + (y(2)-y(4))^2) - m*200;
c(3) = sqrt((x(2) - x(5))^2 + (y(2)-y(5))^2) - m*200;
c(4) = sqrt((x(3) - x(4))^2 + (y(3)-y(4))^2) - m*150;
c(5) = sqrt((x(3) - x(5))^2 + (y(3)-y(5))^2) - m*150;
c(6) = sqrt((x(4) - x(5))^2 + (y(4)-y(5))^2) - m*200;
c = m*sum(c);
end
But I doesn't work correctly, I think I'm using wrong the fmincon function.
I don't know neither how to optimize (-m) ... should I use syms m or something like that?
edit: The output like this is always [0 0 0 0 0 0 0 0 0 0] when it shouldn't. See the output here.
Thank you so much in advice.
Some more observations.
We can simplify things a little bit by getting rid of the square root. So the constraints look like:
set c = {x,y}
maximize m2
m2 * sqrpathlen(ut,vt) <= sum(c, sqr(pos(ut,c)-pos(vt,c))) for all paths ut->vt
where m2 is the square of m.
This is indeed non-convex. With a global solver I get as solution:
---- 83 VARIABLE m2.L = 12.900 m^2, with m=scale
PARAMETER m = 3.592
---- 83 VARIABLE pos.L positions
x y
u3 700.000 700.000
u4 700.000 161.251
u5 161.251 700.000
(pos(u2,x) and pos(u2,y) are zero).
With a local solver using 0 as starting point, we see we don't move at all:
---- 83 VARIABLE m2.L = 0.000 m^2, with m=scale
PARAMETER m = 0.000
---- 83 VARIABLE pos.L positions
( ALL 0.000 )
Good afternoon!
First things first, I looked for similar questions for a while, but (probably because of my inexperience) I've found nothing similar to what I'm going to ask.
I'm using matlab for the first time to solve this kind of problems, so I'm not sure of what to do. A brief explenation:
I'm doing a project for my Optimal Control course: I have to replicate the results of a paper about employment, and I'm stuck with the plots. I have the following data:
five variable functions (U(t), T(t), R(t), V1(t) and V2(t))
four control functions(u1(t), u2(t), u3(t), u4(t))
constraints on the control variables (each u must be between 0 and 1)
initial values for U, T, R, V1 and V2 (in t=0, in particular V1 and V2 are constant over time)
final values for the λ coefficients in the hamiltonian
(note: for the controls, I've already found the optimal expression, which is in this form: ui = min{1, max{0,"expression"}}. If needed, I can give also the four expressions, neglected to
synthesize a little)
Under professor's suggestions, I've tried to use fmincon, that theoretically should give me directly the information that I need to plot some result using only the cost function of the problem. But in this case I have some issues involving time in the calculations. Below, the code that I used for fmincon:
syms u
%note: u(5) corresponds to U(t), but this is the only way I've found to get
%a result, the other u(i) are in ascending order (u(1) = u1 and so on...)
g = #(u) 30*u(5) + (20/2)*(u(1))^2 + (20/2)*(u(2))^2 + (10/2)*(u(3))^2 + (40/2)*(u(4))^2;
%initial guesses
u0 = [0 0 0 0 100000]; %
A = [];
b = [];
Aeq = [];
beq = [];
lb = 0.0 * ones(1,2,3,4);
ub = 1.0 * ones(1,2,3,4);
[x,fval,output,lambda] = fmincon(g, u0, A, b, Aeq, beq, lb, ub);
Whit this code, i get (obviously) only one value for each variable as result, and since I've not found any method to involve time, as I said before, I start looking for other solving strategies.
I found that ode45 is a differential equation solver that has the "time iteration" already included in the algorithm, so I tried to write the code to get it work with my problem.
I took all the equations from the paper and put them in a vector as shown in the mathworks examples, and this is my matlab file:
syms u1(t) u2(t) u3(t) u4(t)
syms U(t) T(t) R(t) V1(t) V2(t)
syms lambda_u lambda_t lambda_r lambda_v1 lambda_v2
%all the parameters provided by the paper
delta = 500;
alpha1 = 0.004;
alpha2 = 0.005;
alpha3 = 0.006;
gamma1 = 0.001;
gamma2 = 0.002;
phi1 = 0.22;
phi2 = 0.20;
delta1 = 0.09;
delta2 = 0.05;
k1 = 0.000003;
k2 = 0.000002;
k3 = 0.0000045;
%these two variable are set constant
V1 = 200;
V2 = 100;
%weight values for the cost function (only A1 is used in this case, but I left them all since the unused ones are irrelevant)
A1 = 30;
A2 = 20;
A3 = 20;
A4 = 10;
A5 = 40;
%ordering the unknowns in an array
x = [U T R u1 u2 u3 u4];
%initial conditions, ordered as the x vector (for the ui are guesses)
y0 = [100000 2000 1000 0 0 0 0];
%system set up
f = #(t,x) [delta - (1 + x(4))*k1*x(1)*V1 - (1 + x(5))*k2*x(1)*V2 - alpha1*x(1) + gamma1*x(2) + gamma2*x(3);...
(1 + x(4))*k1*x(1)*V1 - k3*x(2)*V2 - alpha2*x(2) - gamma1*x(2);...
(1 + x(5))*k2*x(1)*V2 - alpha3*x(3) - gamma2*x(3) + k3*x(2)*V2;...
alpha2*x(2) + gamma1*x(2) + (1 + x(6))*phi1*x(1) + k3*x(2)*V2 - delta1*V1;...
alpha3*x(3) + gamma2*x(3) + (1 + x(7))*phi2*x(1) - delta2*V2;...
-A1 + (1 + x(4))*k1*V1*(lambda_u - lambda_t) + (1 + x(5))*k2*V2*(lambda_u - lambda_r) + lambda_u*alpha1 - lambda_v1*(1 + x(6))*phi1 - lambda_v2*(1 + x(7))*phi2;...
-lambda_u*gamma1 + (alpha2 + gamma1)*(lambda_t - lambda_v1) + k3*V2*(lambda_t - lambda_r - lambda_v1);...
-lambda_u*gamma2 + (alpha3 + gamma2)*(lambda_r - lambda_v2);...
(1 + x(4))*k1*x(1)*(lambda_u - lambda_t) + lambda_v1*delta1;...
(1 + x(5))*k2*x(1)*(lambda_u -lambda_r) + k3*x(2)*(lambda_t - lambda_r - lambda_v1) + lambda_v2*delta2];
%using ode45 to solve over the chosen time interval
[t,xa] = ode45(f,[0 10],y0);
With this code, I get the following error:
Error using odearguments (line 95)
#(T,X)[DELTA-(1+X(4))*K1*X(1)*V1-(1+X(5))*K2*X(1)*V2-ALPHA1*X(1)+GAMMA1*X(2)+GAMMA2*X(3);(1+X(4))*K1*X(1)*V1-K3*X(2)*V2-ALPHA2*X(2)-GAMMA1*X(2);(1+X(5))*K2*X(1)*V2-ALPHA3*X(3)-GAMMA2*X(3)+K3*X(2)*V2;ALPHA2*X(2)+GAMMA1*X(2)+(1+X(6))*PHI1*X(1)+K3*X(2)*V2-DELTA1*V1;ALPHA3*X(3)+GAMMA2*X(3)+(1+X(7))*PHI2*X(1)-DELTA2*V2;-A1+(1+X(4))*K1*V1*(LAMBDA_U-LAMBDA_T)+(1+X(5))*K2*V2*(LAMBDA_U-LAMBDA_R)+LAMBDA_U*ALPHA1-LAMBDA_V1*(1+X(6))*PHI1-LAMBDA_V2*(1+X(7))*PHI2;-LAMBDA_U*GAMMA1+(ALPHA2+GAMMA1)*(LAMBDA_T-LAMBDA_V1)+K3*V2*(LAMBDA_T-LAMBDA_R-LAMBDA_V1);-LAMBDA_U*GAMMA2+(ALPHA3+GAMMA2)*(LAMBDA_R-LAMBDA_V2);(1+X(4))*K1*X(1)*(LAMBDA_U-LAMBDA_T)+LAMBDA_V1*DELTA1;(1+X(5))*K2*X(1)*(LAMBDA_U-LAMBDA_R)+K3*X(2)*(LAMBDA_T-LAMBDA_R-LAMBDA_V1)+LAMBDA_V2*DELTA2]
returns a vector of length 10, but the length of initial conditions vector is 7. The vector returned by
#(T,X)[DELTA-(1+X(4))*K1*X(1)*V1-(1+X(5))*K2*X(1)*V2-ALPHA1*X(1)+GAMMA1*X(2)+GAMMA2*X(3);(1+X(4))*K1*X(1)*V1-K3*X(2)*V2-ALPHA2*X(2)-GAMMA1*X(2);(1+X(5))*K2*X(1)*V2-ALPHA3*X(3)-GAMMA2*X(3)+K3*X(2)*V2;ALPHA2*X(2)+GAMMA1*X(2)+(1+X(6))*PHI1*X(1)+K3*X(2)*V2-DELTA1*V1;ALPHA3*X(3)+GAMMA2*X(3)+(1+X(7))*PHI2*X(1)-DELTA2*V2;-A1+(1+X(4))*K1*V1*(LAMBDA_U-LAMBDA_T)+(1+X(5))*K2*V2*(LAMBDA_U-LAMBDA_R)+LAMBDA_U*ALPHA1-LAMBDA_V1*(1+X(6))*PHI1-LAMBDA_V2*(1+X(7))*PHI2;-LAMBDA_U*GAMMA1+(ALPHA2+GAMMA1)*(LAMBDA_T-LAMBDA_V1)+K3*V2*(LAMBDA_T-LAMBDA_R-LAMBDA_V1);-LAMBDA_U*GAMMA2+(ALPHA3+GAMMA2)*(LAMBDA_R-LAMBDA_V2);(1+X(4))*K1*X(1)*(LAMBDA_U-LAMBDA_T)+LAMBDA_V1*DELTA1;(1+X(5))*K2*X(1)*(LAMBDA_U-LAMBDA_R)+K3*X(2)*(LAMBDA_T-LAMBDA_R-LAMBDA_V1)+LAMBDA_V2*DELTA2]
and the initial conditions vector must have the same number of elements.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in test (line 62)
[t,xa] = ode45(f,[0 10],y0);
For which I can't find a solution, since I have used all the initial values given in the paper. The only values that I have left are the final values for the lambda coefficients, since they are final values, and I am not sure if they can be used.
In this case, I can't also understand where I should put the bounds on the control variable.
For completeness, I will provide also the link to the paper in question:
https://www.ripublication.com/ijss17/ijssv12n3_13.pdf
Can you help me figure out what I can do to solve my problems?
P.S: I know this is a pretty bad code, but I'm basing on the basics tutorials on mathworks; for sure this should need to be refactored and ordered in various file (one for the cost function and one for the constraints for example) but firstly I would like to understand where the problem is and then I will put all in a pretty form.
Thank you so much!
Generally you confused something with Vectors. In initial conditions you declared 7 values:
%initial conditions, ordered as the x vector (for the ui are guesses)
y0 = [100000 2000 1000 0 0 0 0];
But you declared 10 ODE's:
%system set up
f = #(t,x) [delta - (1 + x(4))*k1*x(1)*V1 - (1 + x(5))*k2*x(1)*V2 - alpha1*x(1) + gamma1*x(2) + gamma2*x(3);...
(1 + x(4))*k1*x(1)*V1 - k3*x(2)*V2 - alpha2*x(2) - gamma1*x(2);...
(1 + x(5))*k2*x(1)*V2 - alpha3*x(3) - gamma2*x(3) + k3*x(2)*V2;...
alpha2*x(2) + gamma1*x(2) + (1 + x(6))*phi1*x(1) + k3*x(2)*V2 - delta1*V1;...
alpha3*x(3) + gamma2*x(3) + (1 + x(7))*phi2*x(1) - delta2*V2;...
-A1 + (1 + x(4))*k1*V1*(lambda_u - lambda_t) + (1 + x(5))*k2*V2*(lambda_u - lambda_r) + lambda_u*alpha1 - lambda_v1*(1 + x(6))*phi1 - lambda_v2*(1 + x(7))*phi2;...
-lambda_u*gamma1 + (alpha2 + gamma1)*(lambda_t - lambda_v1) + k3*V2*(lambda_t - lambda_r - lambda_v1);...
-lambda_u*gamma2 + (alpha3 + gamma2)*(lambda_r - lambda_v2);...
(1 + x(4))*k1*x(1)*(lambda_u - lambda_t) + lambda_v1*delta1;...
(1 + x(5))*k2*x(1)*(lambda_u -lambda_r) + k3*x(2)*(lambda_t - lambda_r - lambda_v1) + lambda_v2*delta2];
Every line in above code is recognized as one ODE.
But that's not all. The second problem is with your construction. You mixed symbolic math (lambda declared as syms) with numerical solving, which will be tricky. I'm not familiar with the exact scientific problem you are trying to solve, but if you can't avoid symbolic math, maybe you should try dsolve from Symbolic Math Toolbox?
This question already has answers here:
MATLAB - Fit exponential curve WITHOUT toolbox
(2 answers)
Closed 7 years ago.
I have such x vectors and y vectors described in below :
x = [0 5 8 15 18 25 30 38 42 45 50];
y = [81.94 75.94 70.06 60.94 57.00 50.83 46.83 42.83 40.94 39.00 38.06];
with these values how can i find an coefficients of y = a*(b^x) ??
I've tried this code but it finds for y = a*e^(b*x)
clear, clc, close all, format compact, format long
% enter data
x = [0 5 8 15 18 25 30 38];
y = [81.94 75.94 70.06 60.94 57.00 50.83 46.83 42.83];
n = length(x);
y2 = log(y);
j = sum(x);
k = sum(y2);
l = sum(x.^2);
m = sum(y2.^2);
r2 = sum(x .* y2);
b = (n * r2 - k * j) / (n * l - j^2)
a = exp((k-b*j)/n)
y = a * exp(b * 35)
result_68 = log(68/a)/b
I know interpolation techniques but i couldn't implement it to my solutions...
Many thanks in advance!
Since y = a * b ^ x is the same as log(y) = log(a) + x log(b) you can do
>> y = y(:);
>> x = x(:);
>> logy = log(y);
>> beta = regress(logy, [ones(size(x)), x]);
>> loga = beta(1);
>> logb = beta(2);
>> a = exp(loga);
>> b = exp(logb);
so the values of a and b are
>> a, b
a =
78.8627588780382
b =
0.984328823937827
Plotting the fit
>> plot(x, y, '.', x, a * b .^ x, '-')
gives you this -
NB the regress function is from the statistics toolbox, but you can define a very simple version which does what you need
function beta = regress(y, x)
beta = (x' * x) \ (x' * y);
end
As an extension to the answer given by Chris Taylor, which provides the best linear fit in the logarithmic-transformed domain, you can find a better fit in the original domain by solving directly the non-linear problem with, for example, the Gauss-Newton algorithm
Using for example the solution given by Chris as a starting point:
x = [0 5 8 15 18 25 30 38 42 45 50];
y = [81.94 75.94 70.06 60.94 57.00 50.83 46.83 42.83 40.94 39.00 38.06];
regress = #(y, x) (x' * x) \ (x' * y);
y = y(:);
x = x(:);
logy = log(y);
beta = regress(logy, [ones(size(x)), x]);
loga = beta(1);
logb = beta(2);
a = exp(loga)
b = exp(logb)
error = sum((a*b.^x - y).^2)
Which gives:
>> a, b, error
a =
78.862758878038164
b =
0.984328823937827
error =
42.275290442577422
You can iterate a bit further to find a better solution
beta = [a; b];
iter = 20
for k = 1:iter
fi = beta(1)*beta(2).^x;
ri = y - fi;
J = [ beta(2).^x, beta(1)*beta(2).^(x-1).*x ]';
JJ = J * J';
Jr = J * ri;
delta = JJ \ Jr;
beta = beta + delta;
end
a = beta(1)
b = beta(2)
error = sum((a*b.^x - y).^2)
Giving:
>> a, b, error
a =
80.332725222265623
b =
0.983480686478288
error =
35.978195088265906
One more option is to use MATLAB's Curve Fitting Toolbox which lets you generate the fit interactively, and can also emit the MATLAB code needed to run the fit.
I would like to preface this by saying, I know some functions, including RGB2HSI could do this for me, but I would like to do it manually for a deeper understanding.
So my goal here is to change my RGB image to HSI color scheme. The image is in .raw format, and i am using the following formulas on the binary code to try and convert it.
theta = arccos((.5*(R-G) + (R-B))/((R-G).^2 + (R-B).*(G-B)).^.5);
S = 1 - 3./(R + G + B)
I = 1/3 * (R + G + B)
if B <= G H = theta if B > G H = 360 - theta
So far I have tried two different things, that have resulted in two different errors. The first attempted was the following,
for iii = 1:196608
C(iii) = acosd((.5*(R-G) + (R-B))/((R-G).^2 + (R-B).*(G-B)).^.5);
S(iii) = 1 - 3./(R + G + B);
I(iii) = 1/3 * (R + G + B);
end
Now in attempting this I knew it was grossly inefficent, but I wanted to see if it was a viable option. It was not, and the computer ran out of memory and refused to even run it.
My second attempt was this
fid = fopen('color.raw');
R = fread(fid,512*384*3,'uint8', 2);
fseek(fid, 1, 'bof');
G = fread(fid, 512*384*3, 'uint8', 2);
fseek(fid, 2, 'bof');
B = fread(fid, 512*384*3, 'uint8', 2);
fclose(fid);
R = reshape(R, [512 384]);
G = reshape(G, [512 384]);
B = reshape(B, [512 384]);
C = acosd((.5*(R-G) + (R-B))/((R-G).^2 + (R-B).*(G-B)).^.5);
S = 1 - 3./(R + G + B);
I = 1/3 * (R + G + B);
if B <= G
H = B;
if B > G
H = 360 - B;
end
end
H = H/360;
figure(1);
imagesc(H * S * I)
There were several issues with this that I need help with. First of all, the matrix 'C' has different dimensions than S and I so multiplication is impossible, so my first question is, how would I call up each pixel so I could perform the operations on them individually to avoid this dilemma.
Secondly the if loops refused to work, if I put them after "imagesc" nothing would happen, and if i put them before "imagesc" then the computer would not recognize what variable H was. Where is the correct placement of the ends?
Normally, the matrix 'C' have same dimensions as S and I because:
C = acosd((.5*(R-G) + (R-B))/((R-G).^2 + (R-B).*(G-B)).^.5);
should be
C = acosd((.5*(R-G) + (R-B))./((R-G).^2 + (R-B).*(G-B)).^.5);
elementwise division in the middle was missing . Another point is:
if B <= G
H = B;
if B > G
H = 360 - B;
end
end
should be
H = zeros(size(B));
H(find(B <= G)) = B(find(B <= G));
H(find(B > G)) = 360 - B(find(B > G));