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.
Related
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 2 years ago.
Improve this question
I have an array of structs. Every struct holds a set of data for one single measure. Matlab gives me an error when I try to plot this data.
Expected one output from a curly brace or dot indexing expression, but there were 361 results.
How should I rewrite my plot code?
plot(result.structArray_A(:).nonArrayValue_X, result.structArray_A(:).nonArrayValue_Y);
I have found a solution. Simply writing the following does work, even if the syntax does look odd:
plot([result.structArray_A.nonArrayValue_X], [result.structArray_A.nonArrayValue_Y]);
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(:).'];
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
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
I have a mathematical data where it would be very convenient to have the index to start from zero like
a=sparse([],[],[],30,1);
>> a(0)=someValueHere
Subscript indices must either be real positive integers or logicals.
but Matlab by default offers only the index to start from 1. Is there some easy hack or trick by which I could still assign a(0) so that I don't need to create a dummyVar a0 for the value or append the value at the end?
So how to get assignment such as a(0) in Matlab? Every time zero-index called catch the error and return someValueHere instead of the warning?
To get MATLAB's index to start from 0 you'll need to make an large set of object classes that emulate regular numeric classes, but behave differently with functions such as subsassgn(), subsref() etc.
Maybe someone was crazy enough to do it somewhere, I'd expect this to take weeks to months of work to actually work properly.
There is a discussion on the matlab index issue: http://www.mathworks.cn/matlabcentral/newsreader/view_thread/285566
Maybe you can write a function like
function t=C_index(x)
t = x + 1;
Then you can write something like y(C_index(0)) to get the first value in vector y.
In Addition,
t=#(x) x+1
y(t(0))
should work.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
Given the signals:
f1[n] = sinc[n] {1[n+5]-1[n-5]}
f2[n] = 1-rect[n]
f3[n] = 1[n]-1[n-5]
write a programm in matlab in which you will check the following proprieties:
1)sinc[n]:=sin(phi*n)/phi*n;
2)(f1*f2)[n] = (f2*f1)[n];
3)f1[n]*{ f2[n] + f3[n] } = f1[n]*f2[n] + f1[n]*f3[n];
4)(f1*delta)[n] = (delta*f1)[n] = f1[n];
I'm really really grateful for any tips/ideal on how to solve this problem. :)
sinc[n]:=sin(phi*n)/phi*n;
That certainly isn't Matlab syntax, and the ; at the end makes it not look much like a question either. Anyway, you have two options. Either plot the functions to visually assess equivalence or else check the vectors. I'll demonstrate with this one, then you can try for all the others.
Firstly you need to make a sample n vector which will be your domain over which to test equivalence (i.e. the x values of your plot). I'm going to arbitrarily choose:
n = -10:0.01:10;
Also I'm going to assuming by phi you actually meant pi based on the Matlab definition of sinc: http://www.mathworks.com/help/signal/ref/sinc.html
So now we have to functions:
a = sinc(n);
b = sin(n)./n;
a and b are now vectors with a corresponding "y" value for each element of n. You'll also notice I used a . before the /, this means element wise divide i.e. divide each element by each corresponding element rather than matrix division which is inversion followed by matrix multiplication.
Now lets plot them:
plot(n, a, n, b, 'r')
and finally to check numerical equivalence we could do this:
all(a == b)
But (and this is probably a bit out of scope for your question but important to know) you should actually never check for absolute equivalence of floating point numbers like that as you get precision errors due to different truncations in the inner calculations (because of how your computer stores floating point numbers). So instead it is good practice to rather check that the difference between the two numbers is less than some tiny threshold.
all((a - b) < 0.000001)
I'll leave the rest up to you