Parse error at '=': usage might be invalid Matlab syntax - matlab

Why can't I do this loop this way ?
for i=1:N
(Teta(2,i)-Teta(1,i))/dY=0;
end
I am getting this error : Parse error at '=': usage might be invalid Matlab syntax

You cannot make calculations in the left side of the equals sign. Calculations can only be made in the right-hand side.
For example, like this:
for i=1:N
Teta(2,i) = 0 * dY + Teta(1,i);
end
Without knowing anything about the application of you problem, I just moved everything but the Teta(2,i) from the left to the right. This will not give that error. But probably isn't the result you wish wither...

Related

subscript indices must be either positive integers less than 2^31 or logicals

I'm quite new to Matlab/Octave programming, but have this one issue that I can't seem to solve.
I wrote the following which is actually a quite straight forward calculation on an option price using the Black Scholes Formula (just to give you some background). However, I do constantly get the following error msg:
"subscript indices must be either positive integers less than 2^31 or logicals"
One would think that this explaines it quite nicely and I know there've been questions on it before. The thing that causes troubles, however, is that I am not using any kind of subscript index in my code at all.
Here is my code:
function v=BS_LBO_strike_call(s,T,sigma,r,q,l,alpha)
d1=(log(alpha*l./s) + (r-q-0.5*sigma^2)*T)/(sigma*sqrt(T));
d2=(log(alpha*l./s) - (r-q+0.5*sigma^2)*T)/(sigma*sqrt(T));
d3=(log(alpha*l./s) + (r-q+0.5*sigma^2)*T)/(sigma*sqrt(T));
d4=(log(alpha*l./s) + (r-q-0.5*sigma^2)*T)/(sigma*sqrt(T));
v = exp(-r*T)*s(0.5*sigma^2./(r-q)*(l./s).^(2*(r-q)./sigma^2).*normcdf(d1) - 0.5*sigma^2./(r-q)*alpha.^(-2*(r-q)./sigma^2).*exp((r-q).*T).*normcdf(d2) + alpha.exp*((r-q).*T).*normcdf(d3) - (l./s).*normcdf(d4));
So, I can't seem to figure out what doesn't work out for Octave.
I would highly appreciate if you could maybe shed some light on this. I'm convinced there must be something minor that I overlook
The source of your issues lies in your last line. You have the following:
v = exp(-r * T) * s(0.5 * sigma^2 ....
I think that you have omitted an * between the s and the opening parenthesis because as it is now, everything after that parenthesis is being treated as a subscript into s. This is the root cause of the error you are getting because what follows is likely not an integer or logical.
There is one other point in that line that is likely going to lead to some errors as well. You have the following as part of that statement.
alpha.exp*((r-q).*T) ...
Unless alpha is a struct (I'm sure it's not because you haven't used it that way previously), you will likely want something else besides the . between alpha and exp. Maybe another *?

While loop error with Netwon-Raphson Method

I am trying to find use to Newton-Raphson method to find the roots. It does this by making a guess and then improving the guess after each iteration until you get one of the zeros.
Because the Newton-Raphson method quickly finds the zeros, it gives me a small error immediately and after two or three iterations max it should fail to meet the conditions of the while loop. However, the problem is that when I remove the semi-colon after "error" in my loop, I start getting fractions that should break the while loop, but its like Matlab doesn't know that 123/8328423 is less than 1. It continues to run until I manually force the program to stop running.
How do I fix this? I tried format long, format longe, and using double both in the command window, in the scrip file, and somewhere in the loop.
Thank you in advance for any tips, suggestions, or advice that may help!!
A = [1,2,-4;2,-2,-2;-4,-2,1;];
format longe
% syms x y z
% P = x^4 + 3*x^2*y^2-z^3+y+1;
% feval(symengine,'degree',P,x)
syms x
B = mateigenvalue(A);
f(x) = simplify(matdet(B));
x0 = 1;
error = 10;
while(error > .01)
x1 = x0 - f(x0)/(27*(x0)-3*(x0)^2);
error = abs(((f(x0)-f(x1))/f(x0))*100)
x0 = x1;
end
x0 = double(x0)
I reckon the main problem is with error.
It starts as double but inside the while-loop it turns into a symbolic variable and you can't easily compare symbolic variables with scalar values (the .01 in the while-loop condition).
Check in your workspace if error is symbolic (or type class(error) and check if sym is returned). I guess it is symbolic because a fraction is returned (123/8328423), as instead Matlab treats double values with decimals, not fractions.
If so, try doing (inside the while-loop) a conversion for error that is, under the line
error = abs(((f(x0)-f(x1))/f(x0))*100);
try putting
error=double(error);
So error will be temporarily converted in double and you can easily compare its value with .01 to check the while-loop condition.
Also, it is bad practice to call a variable error since error() is a built-in function in Matlab. By naming a variable error you cannot use the error() function. Same story goes for other built-in functions.

Defining A Differential Equation Using matlabFunction

I am trying solve the differential equation by first putting it in normal, which, if I did it correctly, should be dx/dt = a8 1/3 x - 1/3 b8, where a8 is the second derivative, and b8 is the third derivative. Here is a portion of my code:
matlabFunction( [a8 +x8/3 - b8/3; a8; b8],'vars',{t,[b8;a8;x8]},'file','DE_11')
And here are the errors I get:
Error using sym/cat>checkDimensions (line 75)
CAT arguments dimensions are not consistent.
Error in sym/cat>catMany (line 38)
[resz, ranges] = checkDimensions(sz,dim);
Error in sym/cat (line 27)
ySym = catMany(dim, strs);
Error in sym/vertcat (line 19)
ySym = cat(1,args{:});
I honestly do not know what these messages are hinting at. I was hoping someone could help me decipher these error messages, and show me where I went wrong. My intention is, after these issues have been resolved, to use ode45 to solve the differential equation.
Thank you, and I apologize for my ignorance.
EDIT: Okay, after having aimlessly tried various things, I was able to get it to "work." Here is what I changed it to:
matlabFunction([b8;a8;a8 - b8/3 - x8/3], 'vars',{t8,x8,[b8,a8]},'file','DE_11')
However, I am not really certain as to why that worked, or if its even the correct input. Could someone perhaps show me why it worked? I understand that this [b8;a8;a8 - b8/3 - x8/3] represents a column vector containing my unknown functions, but I do not exactly understand this part {t8,x8,[b8,a8]}. I know that we are defining variables, but why do we use curly brackets, and why are some enclosed in square brackets, and others are not?
the error is caused by
[a8 +x8/3 - b8/3; a8; b8]
as this attempts to create a matrix with inconsistent dimensions as the first space separates column entries in the first row. using no spaces or spaces either side of the operators will solve this...
[a8 + x8/3 - b8/3; a8; b8] or [a8+x8/3-b8/3; a8; b8]
should work as intended.
see http://www.mathworks.co.uk/help/matlab/matlab_prog/symbol-reference.html#bsgigzp-52

plot error in matlab Unbalanced or unexpected parenthesis or bracket

I want plot this function in Matlab:
F(p)=((3/2)*(7.02^2))-(2*18*p((1-(p/18))*(1-(exp(-18/p))))
I tried to make the plot as described in this Mathworks page. And I wrote this :
p=0.001:0.001:10;
F=(((3/2)*(7.02^2))-(2*18*p((1-(p/18))*(1-(exp(-18/p)))));
plot(p,y)
but I got an error:
??? F=(((3/2)*(7.02^2))-(2*18*p((1-(p/18))*(1-(exp(-18/p)))));
|
Error: Unbalanced or unexpected parenthesis or bracket.
I tried also to some loop like this:
p=0.01:0.01:10;
F=zeros(1,length(p))
for i = 1:1000
F(i)=(((3/2)*(7.02^2))-(2*18*p(i)((1-(p(i)/18))*(1-(exp(-18/p(i))))));
end
plot(p,y)
but I got the error :
??? F=(((3/2)*(7.02^2))-(2*18*p((1-(p/18))*(1-(exp(-18/p)))));
|
Error: Unbalanced or unexpected parenthesis or bracket.
??? Error: File: Untitled2.m Line: 4 Column: 70
Unbalanced or unexpected parenthesis or bracket.
I don't understand where the problem is...
You clearly have either too many ( or too many ). Count them, and use the Matlab editor's syntax highlighting and help to find where exactly.
You have a few issues here. The first is the number of ( and ) that you are using. The second is that you aren't accounting for the fact that p is a vector and that requires .* and ./ operators since matrix operations aren't always as simple as saying A times B when the vector lengths don't match.
Either way, this should work for you now...
F=(((3/2)*(7.02^2))-(2*18*p.*(1-(p./18))).*(1-(exp(-18./p))));
In the future it helps to just simple count the number of ( and ) like #rubenvb mentioned. That will give you a great indicator of where the issue might be.
In this particular case you would have gotten the matrix dimension mismatch error due to what I stated about the multiplication and division above.
Hope that helps.

Puzzling "Subscripted assignment dimension mismatch" only during loop using portopt & ewstats

I am tearing my hair out over this one.
I have a set of daily returns of 4 assets, using a 10 day window I loop over the whole dataset (from i = 1 to 50) performing a number of calculations and building optimal portfolios. This involves using portopt.
[PortRisk(:,i), PortReturn(:,i), PortWts(:,:,i)] = portopt(ExpReturn(i,:), ExpCovariance(:,:,i), [], [], ConSet);
The inputs, ExpReturn and ExpCovariance are generated using ewstats
[ExpReturn(i,:), ExpCovariance(:,:,i)] = ewstats(RetSeries, 0.94)
Now, on the final 50th iteration (and only the 50th - all previous work fine), I get the following error:
??? Subscripted assignment dimension mismatch. Error in ==> Script at 10
[PortRisk(:,i), PortReturn(:,i), PortWts(:,:,i)] = portopt(ExpReturn(i,:), ExpCovariance(:,:,i), [], [], ConSet);
Note, I see no issue with RetSeries as ExpReturn and ExpCovariance generated by ewstats are size <50x4> and <4x4x50> respectively.
I have tried everything i can think of to hunt down the error, including checking size(), using breakpoints, preallocating the matrices etc etc. Oddly, if i remove the loop, set i = 50, it works. Furthermore, if instead of ewstats I simply use mean() and cov() - they work on the 50th iteration. If i replace one, ExpReturn for example, with a mean(RetSeries), it works. Similarly, replacing ExpCovariance with cov(RetSeries) - works. But both ExpReturn and ExpCovariance together always fail.
What is causing the error?
EDIT:
Using dbstop if error, I can see:
PortRisk <10x50>
PortReturn <10x50>
PortWts<10x4x49>
ExpReturn <50x4>
ExpCovariance<4x4x50>
so the problem is PortWts but I do not understand why now it is not the right dimensions when it was for 49 other iterations. Also, the offending error line is the first point in the loop PortWts is mentioned, so nothing is messing with it beforehand
Try setting dbstop if error, and run your code again. MATLAB will enter debug mode at the exact point where the error occurs.
Here is a screencast by Doug Hull showing how
EDIT
Change the offending line to:
[a,b,c] = portopt(ExpReturn(i,:), ExpCovariance(:,:,i), [], [], ConSet);
then assign each individually:
PortRisk(:,i) a;
PortReturn(:,i) = b;
PortWts(:,:,i) = c;
Now when it fails, it will show you exactly which output did not have the expected size. Combined with the above trick, you can now inspect the variables in your workspace at the time of the error, and figure out what is wrong..
EDIT2
In addition, add the following test in between the two (before assigning):
if isempty(a) || isempty(b) || isempty(c)
keyboard %# enter debug mode. Or issue an error
end