# I have problem writing a matlab function

2 visualizzazioni (ultimi 30 giorni)
Cantor Set il 19 Ott 2019
Risposto: Image Analyst il 19 Ott 2019
I want to write this function in matrix form.
The function is
I defined the vector as:
Then what is wrong with my code?
function [ ObjVall ] = objfun2( phen)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
x=(5/pi)*phen(1:end,1)-6;
y=phen(1:end,2) - (5.1/(4*pi^2)).*(phen(1:end,1).^2 +x);
z=10*(1 - (1/(8*pi))*cos(phen(1:end,1)));
ObjVall=(y).^2 + z +10;
end
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposte (3)

the cyclist il 19 Ott 2019
Modificato: the cyclist il 19 Ott 2019
The coefficient
(5.1/(4*pi^2))
is multiplying the term you have called x, which is not correct.
It might have been easier to debug if you define
x1 = phen(:,1);
x2 = phen(:,2);
inside your function, which will make your MATLAB expression look more like the math equation.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Sedi Ghan il 19 Ott 2019
I don't know where is wrong in your code. However, I write your formula as follows and it works.
function [ ObjVall ] = objfun2( phen)
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
ObjVall= (phen(:,2) - (5.1/(4*pi^2))*phen(:,1).^2 + (5/pi)*phen(:,1)-6 ).^2+...
+10*(1-(1/8*pi)*cos(phen(:,1))) + 10;
end
When phen is an N by 2 matrix, the output is an N by 1 array.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

Image Analyst il 19 Ott 2019
Make sure x1 and x2 are column vectors (Nx1), NOT row vectors (1xN).
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su Logical in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by