Using loop in fsolve and plugging the result to a function - matlab

I have functions which has totally 4 unknowns (2x2).
I got the solution. But what I am going to do is to vary some parameters so that I can see how the original solution changes.
But matlab keeps saying that in A(I)=B, 'I' and the number of element B must be the same.
Example code could be such as( in the code, psi01 psi02 psi03 psi04 are the varying parameters);
R=902;
psi01=0.9:-0.1:0.1;
psi11=0.9:-0.1:0.1;
psi02=0.1:0.1:0.9;
psi12=0.1:0.1:0.9;
E0=[R/4 R/4; R/4 R/4];
for i=1:9
vv=#(E) [
g/E(2,1)-(th1+psi12(i)/psi11(i)*th2)-((psi01(i)/psi11(i))+a*b)/b*(g/E(1,1)-(rho*b* (psi11(i)/psi01(i))/(psi01(i)/psi11(i)+a*b))*(th1+psi12(i)/psi11(i)*th2)*(th1+psi02(i) /psi11(i)*th2) );
g/E(2,2)-(psi11(i)/psi12(i)*th1+th2)-((psi02(i)/psi12(i))+a*b)/b*(g/E(1,2)-(rho*b*(psi12(i)/psi02(i))/(psi02(i)/psi12(i)+a*b))*(psi11(i)/psi12(i)*th1+th2)*(psi01(i)/psi02(i)*th1+th2) );
R-(E(1,1)+E(1,2)+E(1,1)+E(2,2));
E(1,2)- c(E)*E(1,1);
E(2,2)- d(E)*E(2,1)
];
Ep2(i)=fsolve(vv, E0);
end

If the result of fsolve is a 2x2 array, then how can you expect to save it as a scalar?
Ep2(i)=fsolve(vv, E0);
Ep2 is being used here to store a single element, a scalar. You can't stuff 4 elements into one, and not get an error.
Use a 3-dimensional array, a struct, or use a cell array.

Related

vectorize a function with multiple variables

Consider the arbitrary function:
function myFunc_ = myFunc(firstInput, secondInput)
myFunc_ = firstInput * secondInput;
end
Now imagine I want to map the above function to an array for the first input firstInput, while the second input secondInput is constant. For example, something like:
firstVariable = linspace(0., 1.);
plot(firstVariable, map(myFunc, [firstVariable , 0.1]))
where 0.1 is an arbitrary scalar value for the secondInput and firstVariable array is an arbitrary array for the firstInput.
I have looked into the arrayfun() function. However, I don't know how to include the constant variable. Plus it seems like the syntax between MATLAB and Octave are different, or maybe I'm mistaken. It is important for me to have a cross-compatible code that I can share with colleagues.
Assuming in the original function you were multiplying two scalars and you want to vectorise, then
function myFunc_ = myFunc(firstInput, secondInput)
myFunc_ = firstInput .* secondInput;
end
should work just fine.
Then plot it directly:
plot( firstVariable, myFunc(firstVariable , 0.1) )
I'm afraid the arbitrary examples given in the original question were too simplified and as a result, they do not represent the actual issue I'm facing with my code. But I did manage to find the right syntax that works inside Octave:
plot(firstVariable, arrayfun(#(tempVariable) myFunc(tempVariable, 0.1), firstVariable))
basically the
#(tempVariable) myFunc(tempVariable, 0.1)
creates what is so-called an anonymous function and the
arrayfun(<function>, <array>)
maps the function over the given array.

MATLAB: Using get in cellfun for a cell array of objects

I've been stuck with this for a while and I couldn't find something similar asked previously (or I have failed in doing so)
My situation is fairly simple: I have a cell array of objects. They are all the same object and I have a get function for this kind of object which is: get (obj, attr), where obj is the object in question and attr is a integer from 1-6. Depending on the number the get function returns the corresponding attribute.
I would like to obtain all of my "position" attributes from all my objects which are in the corresponding cell array (this would be attr = 2). I know that cellfun performs a function on all cells, but the question is, how do I use my get function here for all my objects, taking into account that the function is get (obj, attr) ?
Thanks in advance
Firstly, by using get as a custom function you are shadowing the built-in get function - this is bad practise!
With this in mind, and to avoid confusion with the built-in get function which has similar syntax, I'm going to use getattr as a stand-in for your custom function which accpets an object and an integer 1 to 6.
pos = cellfun( #(obj) getattr( obj, 2 ), myCellOfObjects, 'uni', 0 );
By specifying 'uni', 0, the output doesn't have to be scalar and will be put into a cell array. This is useful when, for example, you have a multi-element array for your position.
This is equivalent to the following loop:
pos = cell( numel(myCellOfObjects), 1 );
for ii = 1:numel(pos)
pos{ii} = getattr( myCellOfObjects{ii}, 2 );
end
If ever in doubt about cellfun or arrayfun, just write a loop first - they are essentially the same but more concise.
There is a trick to this some are unaware of: you can pass multiple arguments to cellfun like this:
cellfun(#(obj,attr) get(obj,attr), {obj1,obj2},{attr1,attr2},'uni',0)
if you want to get one attribute of the cellarray (instead of providing an attribute for every object in the cellarray), then you can simply use this
cellfun(#(x) getattr(x,attr),obj,'uni',0)
put into anonymous function for convenience:
get_attr = #(obj,attr) cellfun(#(x) getattr(x,attr),obj,'uni',0)
%use:
get_attr(obj_in_cellarray,'myattribute')
%returns cell array of object attributes
I haven't run any of these functions since you didn't provide any example data / code. Please test and feedback.

Finding all occurences of a string within a cell array (itself part of a struc)

I have the following structure
dataDens =
dens: [1x172 double]
level: {1x172 cell}
raga: {1x172 cell}
within which dataDens.raga consists of (reducing the number of columns below for simplicity)
Columns 1 through 3
'Multani' 'Tori' 'Tori'
I'd like to find the indices at which 'Tori' appears (that is, [2 3] for the example above). However, all of the commands I tried (below) either give an error, or return blank outputs. I think it's probably just a matter of adding/removing a curly bracket somewhere, or using some conversion; but I am at my wit's end, and hope someone can help clarify
indices = find(strcmp([dataDens.raga{:}], {'Tori'}))
indices = ismember('Tori', dataDens.raga)
[if,where] = ismember('Tori', dataDens.raga)
The issue had indeed to do with brackets. [dataDens.raga{:}] will result in concatenation of the character arrays, like so:
>> [dataDens.raga{:}]
ans =
'MultaniToriTori'
Using strcmp to compare this to 'Tori' will result in false, since there is no exact match. You can however compare the entire cell using strcmp, which will then return a boolean array, in which find can be used to obtain the indices of true entries:
indices = find(strcmp(data, {'Tori'}))
Alternatively, ismember would also work, but using ismember('Tori', dataDens.raga) you are checking whether 'Tori' is in dataDens.raga, not the other way around. Changing this to
ismember(dataDens.raga, 'Tori')
would again give you a boolean array, and find will obtain the indices you are looking for.

Designing Function to Reduce Return Variables in Matlab

This is NOT a question where I need to know how to add A+B in MATLAB. This is more of a code design question.
I have few function files that return a numeric matrix and index info on the matrix. For example
function [Mat1, IdxID, IdxDate, IdxVal, IdxMarker, IdxOpen, ...] = First ()
....
.... % where IdxId = 1 ; IdxDate = 2 ; ...
end
function [Mat1, IdxUid, IdxName, IdxVal, Mat2, IdxUid2, IdxSalary2, ...] = Second ()
....
.... % where IdxUid= 1 ; IdxName= 2 ; ...
end
As you can see the code becomes clunky and when I call these functions, I have to declare an equal number of outputs to catch all the indices. The advantage is if I suddenly swap ID & Date columns, the calling functions do not change as I simply make ID=2, Date=1. I also have the advantage of renaming these variables inside the function.
Is there a better way to do this? I'm testing whether struct or cell can be used for indices. I can't use datasets or cell for returning numeric matrix. Too much time is lost in translating it into numbers. Thanks.
Yes, you can return arrays/cells/structs instead. For instance, id can be a struct with multiple variables. Your function definition could be as follows.
function [Mat, Id] = Second ()
...
end
In your function, have the following set:
Id.Name
Id.Val
Id.Salary
...
If you find that you have multiple structs with the same exact structure, you can even consider objects.
Please clarify with more details on the structure if you want a more detailed answer.

going from command window (as script) to function

the program is working perfectly but when i am changing it to function the follwing error is beeing displayed:
[Parent1index, Parent1Position, alldcel] = Parent1n(TotalnoOfGrids, noOfNodes, Penalties, test)
??? Index exceeds matrix dimensions.
Error in ==> Parent1n at 10
[~,index]=min(alldcel{t});
Because alldcell{t} may not exist for some values of t if the condition to assign values to it in
if Penalties{t}(r)== 0;
alldcel{t}(r)=inf;
end
is never satisfied. Assume for some t that all values of Penalties{t} are different than zero. Then you would never assign inf to alldcell{t}. That means, you are only extending the cell array alldcell when Penalties{t} is zero for some r. If the condition is never satisfied, alldcell{t} will not exist and asking for it will give you a cell array error.
You should at least initialize it using alldcell = cell(TotalnoOfGrids,1).
Also, comparing for equality to zero using a==0 is not a good idea. You should use abs(a)<tol for some small value tol.
ok with this code the functiion worked:if Penalties{t}(r)> 0;
alldcel {t}(r)=alldcel{t}(r);
else alldcel {t}(r)=inf; but interchanging if statement with else did not