I have a structure that store lots of variable. I want to find function value of these x values. But there is wrong result. My variables :
StartingValue.x ={};
StartingValue.fn =[];
My function is
f = @(x) sin(5.1*pi.*x+0.5).^6;
I created a nested structure and i want to function value of this variable.
StartingValue(1).Next(1).x
= Columns 1 through 6
[0.0025] [0.0075] [0.0125] [0.0175] [0.0225] [0.0275]
Columns 7 through 10
[0.0325] [0.0375] [0.0425] [0.0475]
To find function values i wrote this code:
StartingValue(1).Next(1).fn=cellfun(f,StartingValue(1).Next(1).x)
ans =
Columns 1 through 8
0.0185 0.0385 0.0716 0.1213 0.1899 0.2781 0.3838 0.5023
Columns 9 through 10
0.6263 0.7464
This result is not correct according to mathematical calculation. I couldn't find what mistake i did. I used cellfun because of x variable is a cell array. I didn't understand whether problem is nested structure or cellfun. If my question is not clear, i can post all code.

 採用された回答

Guillaume
Guillaume 2016 年 7 月 28 日

0 投票

The result is entirely correct. Perhaps you're assuming that sin works in degrees? If that is the case, use sind instead of sin.
By the way, there is no reason to use a cell array for your example, a vector would make the code a lot simpler:
StartingValue(1).Next(1).x = [0.0025, 0.0075, 0.0125, 0.0175, 0.0225, 0.0275, 0.0325, 0.0375, 0.0425, 0.0475]
f = @(x) sin(5.1*pi.*x+0.5).^6;
StartingValue(1).Next(1).fn = f(StartingValue(1).Next(1).x)
Also note that the fn field name is misleading since its content is not a function but a vector.

5 件のコメント

Ozge Moral
Ozge Moral 2016 年 7 月 29 日
I need to use cell arrays because i will concatenate arrays in another part of code. First part of my code is
f = @(x) sin(5.1*pi.*x+0.5).^6;
StartingValue = initialization(0,1,20);
StartingValue.fn = cellfun(f,StartingValue.x);
When i works as only StartingValue.fn with same x values above, results :
ans =
Columns 1 through 8
0.2316 0.9498 0.0449 0.0000 0.3065 0.8929 0.0261 0.0001
Columns 9 through 16
0.3916 0.8190
But after i created nested structure StartingValue.Next, and run it for same x values, results(my original question results i mean ) was different above.Therefore i thought results are not correct.
Guillaume
Guillaume 2016 年 7 月 29 日
Not knowing what initialization does, it's difficult to diagnose the problem, but applying f to 0.0025 should result in ~0.01848, not ~0.2316.
Ozge Moral
Ozge Moral 2016 年 7 月 29 日
function [ StartingValue] = initialization( a,b,n )
% [a,b] is interval of function that minimum points will search.
% n is pre-assigned number, # of subinterval.
% Intervals and midpoint of these intervals are described.
% a=a;
% b=b;
% n=n;
sz=(b-a)/n;
StartingValue.interval = {};
StartingValue.x ={};
StartingValue.fn =[];
StartingValue.eta =[];
StartingValue.allowed = [];
StartingValue.Ants = ones(n,1);
for i=1:n
StartingValue.interval{i} = [a+(i-1)*sz a+i*sz];
StartingValue.x{i} = a+(i-0.5)*sz;
end
end
It describes initial values only. I couldn't find why result is wrong. Thank you a lot for interesting.
Guillaume
Guillaume 2016 年 7 月 29 日
There's a factor of 10 difference between the values generated by your initialization function, and the values in your original post:
0.025 vs 0.0025
This explains the vastly different results.
Ozge Moral
Ozge Moral 2016 年 7 月 29 日
編集済み: Ozge Moral 2016 年 7 月 29 日
i'm going to show my eyes to doctor! Thanks a lot.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Structures についてさらに検索

質問済み:

2016 年 7 月 28 日

編集済み:

2016 年 7 月 29 日

Community Treasure Hunt

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

Start Hunting!

Translated by