How can I get rid of the error "Expression or statement is incorrect--possibly unbalanced (, {, or [." (New To MatLab)
8 views (last 30 days)
Show older comments
I am completely new to MatLab. It is a part of a workshop I am taking and I am utterly lost. I was told to literally follow a pre-made program or whatever it is called and just press run. However, It doesn't work at all and I keep getting "Expression or statement is incorrect--possibly unbalanced (, {, or [.". I am completely clueless and It looks exactly like it does on the paper. I am not sure as to what I am doing wrong.
It starts with...
How to create Fig. 3.3 in Matlab. The program below should first be saved as the file distr_shift.m. Then, calling this function in the following way shift=distr_shift(0 .1, 15, 1, 15) will produce Fig. 3.3 a, and also gives the answer that shift of the mean equals precisely 0.1
So i am not sure if I write the shift=distr_shift(0 .1, 15, 1, 15) FIRST and then the function or I just don't even put it in...?
function [shift,f0;f1;z]=distr_shift(variance,initial_mean,a,b)
z=linspace(13, 17, 201);
%we create 201 different values of z that lie between 13 and 17
f=1/(2*sqrt(variance))*exp(-0.5((z-initial_mean)/(sqrt (variance))).^2);
f0=f/sum(f);
r=a*(z-b);
f1=f0.*exp(r);
f1=f1./sum(f1);
plot (z,f0,z,f1)
new_mean=sum(f1.*z);
shift=new_mean-initial_mean;
My result is this.
Error: File: distr_shift.m Line: 1 Column: 19 Expression or statement is incorrect--possibly unbalanced (, {, or [.
I apologize if any of this is confusing, I am not sure how to even explain it and I have no idea what I am doing.
0 Comments
Answers (1)
Star Strider
on 26 Feb 2016
Edited: Star Strider
on 26 Feb 2016
The problem are the semicolons ‘;’ in the output vector of the function in the function code:
function [shift,f0;f1;z]=distr_shift(variance,initial_mean,a,b)
^ ^
Replacing them with commas should work:
function [shift,f0,f1,z]=distr_shift(variance,initial_mean,a,b)
Semicolons can be used in a matrix to separate rows, and at the end of a line to suppress output, but nowhere else (at least to the best of my knowledge).
0 Comments
See Also
Categories
Find more on Linearization in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!