combining strings by using loops

1 view (last 30 days)
Ronaldo
Ronaldo on 9 Nov 2012
I want to use genetic algorithm (from global optimization toolbox) to optimize a function. My function is f(x1,x2,x3)=a.*x1^2+b.*x2^2+c./x3
I have a database (1000*4) in which 1000 is the number of observations and 4 is the number of variables+Output; i.e, x1, x2, x3 and a measured (I got it by doing experiments in my lab) value which is the result of x1,x2 and x3. I will call the forth column the Outputs
I want to minimize F (by finding the best-fit a,b and c) which is the difference between the measured values and predicted values by f function. To minimize the F function I must write F as follow:
F=((a.*x1(1,1)^2+b.*x2(1,2)^2+c./x3(1,3))-Outputs(1,1)+...
a.*x1(2,1)^2+b.*x2(2,2)^2+c./x3(2,3))-Outputs(2,1)+...
.
.
.
a.*x1(1000,1)^2+b.*x2(1000,2)^2+c./x3(1000,3))-Outputs(1000,1))

Answers (2)

Walter Roberson
Walter Roberson on 9 Nov 2012
F = sum( a .* x1(:,1).^2 + b.* x2(:,2).^2 + c ./ x3(:,3) - Outputs(:,1) );
  1 Comment
Ronaldo
Ronaldo on 9 Nov 2012
I tried your answer. Unfortunately, it is not working. The genetic algorithm code cannot optimize the function that you proposed. I need a procedure that I can get the following result from this input:
Input:
f(x1,x2,x3)=a.*x1^2+b.*x2^2+c./x3
Result:
F=((a.*x1(1,1)^2+b.*x2(1,2)^2+c./x3(1,3))-Outputs(1,1)+...
a.*x1(2,1)^2+b.*x2(2,2)^2+c./x3(2,3))-Outputs(2,1)+...
.
.
.
a.*x1(1000,1)^2+b.*x2(1000,2)^2+c./x3(1000,3))-Outputs(1000,1))

Sign in to comment.


Muruganandham Subramanian
Edited: Muruganandham Subramanian on 9 Nov 2012
N=1000;
F=0;
for i=1:N
if i==1
F(i)=F(i) +a.*x1(i,1)^2+b.*x2(i,2)^2+c./x3(i,3))-Outputs(i,1);
else
F(i)=F(i-1) +a.*x1(i,1)^2+b.*x2(i,2)^2+c./x3(i,3))-Outputs(i,1);
end
F1=sum(F);
  1 Comment
Ronaldo
Ronaldo on 9 Nov 2012
No, this is not the answer. Let me make the question more clear. I do not need the summation value of function F. I need a procedure to make the following big string (not its value). F=((a.*x1(1,1)^2+b.*x2(1,2)^2+c./x3(1,3))-Outputs(1,1)+...
a.*x1(2,1)^2+b.*x2(2,2)^2+c./x3(2,3))-Outputs(2,1)+...
.
.
.
a.*x1(1000,1)^2+b.*x2(1000,2)^2+c./x3(1000,3))-Outputs(1000,1))

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!