How to define a function with a matrix [closed] - matlab

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I want to define a two variables function g so that g(x,y) be a 2*2 matrix. To do this, I define g(x,y)=[1,1;x,y] but when I put g(1,1) I don't get any answer. How can I evaluate to g?

The code g(x,y)=[1,1;x,y] itself will not do anything. I assume that your expect result will be g=[1,1,1,1]? Therefore you should do as follow:
g=g_func(1,1);
disp(g)
function g=g_func(x,y)
g=[1,1;x,y];
end

It's not that different from the previous answer, but perhaps an anonymous function would meet your needs:
>> g = #(x,y)[1,1;x,y];
>> g(5,6)
ans =
1 1
5 6
Alternatively, if you want g to accept only one input (i.e. a 2-element vector, instead of two scalars), you could do:
g = #(x)[1,1;x(1),x(2)];
% or
g = #(x)[1,1;x(:).'];

Related

Failure in initial objective function evaluation. FSOLVE cannot continue [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have this code:
B=[0,0.0574612898073843,0.110961034961411,0.157844338275811,0.198326084545702,0.233505068208522,0.264466281579592,0.291951607426117,0.316302322044398,0.337552164906992,0.355641291820062];
E=[0.00821682460973364,0.0230168805223958,0.0353571800836649,0.0452377232935416,0.0526585101520253,0.0576195406591166,0.0601208148148149,0.0601623326191205,0.0577440940720333,0.0528660991735539,0.0454999999999998];
F=[0.0789623507805327,0.0592582389552085,0.0395296398326703,0.0197765534129168,-1.02030405058073e-06,-0.0198030813182328,-0.0396296296296297,-0.0594806652382411,-0.0793561881440668,-0.0992561983471074,-0.119200000000000];
Q0=[0.248000000000000,0.256216824609734,0.279233705132129,0.314590885215794,0.359828608509336,0.412487118661361,0.470106659320478,0.530227474135293,0.590389806754413,0.648133900826446,0.701000000000000];
Q1=[0.448000000000000,0.535179175390266,0.617454294867871,0.692341114784206,0.757355391490664,0.810012881338639,0.847829340679523,0.868320525864708,0.869002193245587,0.847390099173554,0.801000000000000];
X0=ones(1,11);
A=fsolve(#(X)Func(X,B,E,F,Q0,Q1),X0,optimoptions('fsolve','Display','iter','MaxIterations',10000,'MaxFunctionEvaluations',50000,'FunctionTolerance',10^-8,'Diagnostic','on'));
tmp = -((Q1-Q0*exp(A))./(E+((E*A+F)*(0.5+(A./6)+((A.^2)/24)))));
differenza=B+tmp
function [Y] = Func(X,B,E,F,Q0,Q1)
Y= B-((Q1-Q0*exp(X))./(E+((E*X+F)*(0.5+(X./6)+((X.^2)/24)))));
end
When I run it, it says:
Failure in initial objective function evaluation. FSOLVE cannot continue.
Your matrix dimensions do not agree, you can see that if you just call Func with your X0.
Is Func supposed to return a scalar or a vector?
If it is supposed to return a vector, then perhaps you meant to use element multiplication (.*) rather than matrix multiplication (*)?
B-((Q1-Q0.*exp(X))./(E+((E.*X+F).*(0.5+(X./6)+((X.^2)/24)))))
If Func is to return a scalar then you need to think about the vector dimensions and transpose (') some of your variables.

Derivative of a function in Matlab [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I tried to take the derivative the function, but I can't find my mistake:
syms x
A = -1.6*x^2+18.7*x+3.4
This returns (187*x)/10 - (8*x^2)/5 + 17/5.
Then, diff(A) yields 187/10 - (16*x)/5.
There is no mistake here. The derivative of a second degree polynomial is a first degree polynomial... hence the variable x is still present in the result and you cannot evaluate it numerically unless you give a value to x:
vpa(subs(diff(A),x,4)) % evaluates the derivative for X=4, yields 5.9
If you want to reduce your function to a scalar value, a second order derivative must be taken:
vpa(diff(A,2)) % this returns: -3.2
Finally, if you just feel that the numerical parts of the result are "messy" and should be evaluated, you can call the vpa function on the derivative:
vpa(diff(A)) % this returns: 18.7 - 3.2*x

Matlab: How do you implement this sum? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I need to implement the following:
x_i = e ^ (-1 - sum (y_j * A_ji))
where i = 1..10, j = 1..5 and A is a 5x10 matrix (randomly generated).
I tried using symsum but it gave me an index error. Could someone please help me figure out how to implement this?
With
A = rand(5,10); %# random 5x10 array
y = rand(1,5); %# random 1x5 array
Your sum becomes
x = exp( -1 - y*A);
thanks to linear algebra.

Error in MATLAB's plot function [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have this function
h45 = #(x) 1 / (1 + exp(-thDesnorm' * [1 45 x]'));
where thDesnorm is:
[-23.6608
0.1919
0.1866]
When i want to plot this function this way:
domain = 0:1:100;
figure;
plot(domain, h45(domain));
I get this error:
Error using *
Inner matrix dimensions must agree.
Error in #(x)1/(1+exp(-thDesnorm'*[1,45,x]'))
This function works when calling it with, for example, h45(1).
I guess than in plotting, the function is receiving all the domain vector as the parameter x, and not one by one the values of this vector.
As you guessed, the parameter x is your full array domain. This obviously causes a dimensional error in the dot product thDesnorm'*[1,45,x]. A quick fix would be using arrayfun to evaluate h45. For instance:
thDesnorm = [-23.6608
0.1919
0.1866];
h45 = #(x) 1 / (1 + exp(-thDesnorm' * [1 45 x]'));
domain = 0:1:100;
figure;
plot(domain, arrayfun(#(x)h45(x),domain)) % See modification in h45 call

Whats wrong with this Matlab function [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
function y = CramersRule(A,b)
[m,n] = size(A);
[o,p] = size(b);
if m~=2 | n~=2 | o~=2 | p~=1
error('the matrices must be 2*2 and 2*1')
A=[a b;c d]
b=[e;f]
X = det([e b;f a])/det([a b;c d])
Y = det([a e;c f])/det([a b;c d])
end
end
I just get the result: CramersRule([1 2;3 4], [0;4]), which was an example I used to test it.
There are several strange things here:
First of all you have an if statement that contains an error, but even though there is an error you still do things inside the same statement, perhaps you wanted an else somewhere?
Secondly you use A=[a b;c d] while a, c and d are not even defined.
Thirdly you assign to X and Y which are never even used.
Lastly you ask y as an output argument, whilst there is never an assignment to this. Perhaps you don't realize that matlab is case sensative?
All in all it is just a strange function now. Don't forget to check the mlint (warnings on the right hand side of your screen) as it can pick up most of these things.