Octave parsing error - matlab

I making a basic bisection code, but a taking an error as:
>> asdfg
parse error near line 22 of file C:/Octave/Octave-4.2.1/bin\asdfg.m
syntax error
my code is :
a = [1 -9 -2 120 -130]
x0=1
x1=2
e=0,0132
while abs(x0-x1)/2 > e
gcc = (x0+x1)/2;
if polyval(a,x0)*polyval(a,gcc) < 0
x1=gcc;
else if polyval(a,x1)*polyval(a,gcc) < 0
x1=gcc;
else
printf("asfd")
end
end
BTW i made the code like matlab syntax as i know. I just know they are same syntax.

else if polyval(a,x1)*polyval(a,gcc) < 0
should be
elseif polyval(a,x1)*polyval(a,gcc) < 0
No space between else and if in MATLAB/Octave.

Related

get values from a text file with a mix of floats and strings

I am struggling with a text file that I have to read in. In this file, there are two types of line:
133 0102764447 44 11 54 0.4 0 0.89 0 0 8 0 0 7 Attribute_Name='xyz' Type='string' 02452387764447 884
134 0102256447 44 1 57 0.4 0 0.81 0 0 8 0 0 1 864
What I want to do here is to textscan all the lines and then try to determine the number of 'xyz' (and the total number of lines).
I tried to use:
fileID = fopen('test.txt','r') ;
data=textscan(fileID, %d %d %d %d %d %d %d %d %d %d %d %d %d %s %s %d %d','\n) ;
And then I will try to access data{i,16} to count how many are equal to Attribute_Name='xyz', it doesnt seem to be an efficient though.
what will be a proper way to read the data(what interests me is to count how many Attribute_Name='xyz' do I have)? Thanks
You could simply use count which is referenced here.
In your case you could use it in this way:
filetext = fileread("test.txt");
A = count(filetext , "xyz")
fileread will read the whole text file into a single string. Afterwards you can process that string using count which will return the occurrences from the given pattern.
An alternative when using older versions of MATLAB is this one. It will work with R2006a and above.
filetext = fileread("test.txt");
A = length(strfind(filetext, "xyz");
strfind will return an array which length represents the amount of occurrences of the specified string. The length of that array can be accessed by length.
There is the option of strsplit. You may do something like the following:
count = 0;
fid = fopen('test.txt','r');
while ~feof(fid)
line = fgetl(fid);
words = strsplit( line )
ind = find( strcmpi(words{:},'Attribute_Name=''xyz'''), 1); % Assume only one instance per line, remove 1 for more and correct the rest of the code
if ( ind > 0 ) then
count = count + 1;
end if
end
So at the end count will give you the number.

Creating a for-loop that stores values in a new variable

I'm quite new to Matlab so excuse me for the basic question.
I need to make a for-loop that repeats it's self 384 times.
So :
for i=1:384
I now need the for loop to check if 2 certain variables have the value 1 through 10, and then let them store this in a new variable with that value.
So:
if x==1
somevariable = 1
elseif x== 2
saomevariable = 2
..
..
..
elseif y = 1
someothervariable = 1
etc etc.
Is there a way to write this more efficient?
Thank you!
The first think you can do is:
if(x >= 1 && x <= 10)
somevariable=x;
end
if(y >= 1 && y <= 10)
someohtervariable=y;
end
If you could post more information about "x" and "y", perhaps your script can be further "improved".
Hope this helps.

I got error message about simulink "Output argument is not assigned on some execution paths"

In simulink, I made some model using "MATLAB function"block
but I met error message here.
here is code and error message.
function [VTAS,postVTAS]=fcn(mode,initialVTAS,a,t,preVTAS)
if mode == 1
VTAS = initialVTAS + (a * t) ;
postVTAS = VTAS;
elseif mode == 2
datasize = length(preVTAS);
lastvalue = preVTAS(datasize);
VTAS = lastvalue + 0;
postVTAS = VTAS;
end
end
Output argument 'VTAS' is not assigned on some execution paths.
Function 'MATLAB Function' (#36.25.28), line 1, column 26:
"fcn"
Launch diagnostic report.
I think there is no problem about output "VTAS"
please teach me what is a problems.
As the compiler tells you, under some circumstances there is no output value assigned to VTAS. The reason is that you only assign values to that output if mode is 1 or 2. The compiler doesn't know what values are feasible for mode. To remedy this, you need to make sure that VTAS is assigned under any and all circumstances.
This could be accomplished by, e.g. adding an else construct, like so:
function [VTAS,postVTAS]=fcn(mode,initialVTAS,a,t,preVTAS)
if mode == 1
VTAS = initialVTAS + (a * t) ;
postVTAS = VTAS;
elseif mode == 2
datasize = length(preVTAS);
lastvalue = preVTAS(datasize);
VTAS = lastvalue + 0;
postVTAS = VTAS;
else
VTAS = NaN;
postVTAS = NaN;
end
end
Edit:
Additionally, it would be good practice for the else case to throw an error. This would be helpful for debugging.
As a minor note, for every case, postVTAS is equal to VTAS, so essentially it is superfluous to return both from the function.

Error attempting to use initial conditions

I'm doing a quick problem in Maple with a differential equation and a few initial conditions, but I'm getting an error message that I can't seem to understand given the context. Can anyone quickly elaborate on what's going on here? How do I fix this issue?
> KVLl2 := -4*(i2(t)-2)-12*(i2(t)-i3(t)) = 0;
-16 i2(t) + 8 + 12 i3(t) = 0
> KVLl3 := -12*(i3(t)-i2(t))-4*i3(t)-3.5*(diff(i3(t), t)) = 0;
/ d \
-16 i3(t) + 12 i2(t) - 3.5 |--- i3(t)| = 0
\ dt /
> mySoln := dsolve({KVLl2, KVLl3, i2(0) = 1, i3(0) = 1}, i2, i3);
Error, (in dsolve) found the following equations not depending
on the unknowns of the input system: {1 = 1}
Thanks in advance
Maple doesn't know what to do with i2 and i3 you provided as target functions. If you look at the help of dsolve (?dsolve), you see that it requires its target functions to be specified in terms of their variables (t in this case) and as a list. Try using this
dsolve({KVLl2, KVLl3, i2(0) = 1, i3(0) = 1}, {i2(t), i3(t)});
No errors here but no solution either (this might be related to your equation)

Implementation of correlation matrix in MATLAB

I want to implement the following formula in MATLAB, where u_i^(k) means the i,k element. However, I get different results from the ones I compute by hand... I believe that something is wrong with my MATLAB code. For instance,
I should get:
L_ii =
0.1022 0 0
0 0.1657 0
0 0 2.7321
U_ij =
0.7514 0.3104 0.5823
-0.6513 0.4901 0.5793
-0.1055 -0.8145 0.5704
1,1=1-(0.1022*(+0.7514)^2+0.1657*(+0.3104)^2+2.7321*(+0.5823)^2)=-0.000049
2,2=1-(0.1022*(-0.6513)^2+0.1657*(+0.4901)^2+2.7321*(+0.5793)^2)=-0.000015
3,3=1-(0.1022*(-0.1055)^2+0.1657*(-0.8145)^2+2.7321*(+0.5704)^2)=+0.000030
Any ideas??? Please, help me fix Epsilon first (it might not need to move on Rho. Let's fix Epsilon first...)
EDIT: Here is a sample code:
E_squared_ii = ONES_j - diag(L_ii)' * (U_ij'.^ 2)
And here is the wrong result I get at the moment:
E_squared_ii =
1.0e-15 *
0.444089209850063 0.333066907387547 -0.222044604925031
If I use your values and code, I get the expected result:
>> L_ii
L_ii =
0.1022 0 0
0 0.1657 0
0 0 2.7321
>> U_ij
U_ij =
0.7514 0.3104 0.5823
-0.6513 0.4901 0.5793
-0.1055 -0.8145 0.5704
>> ONES_j
ONES_j =
1 1 1
>> E_squared_ii = ONES_j - diag(L_ii)' * (U_ij'.^ 2)
E_squared_ii =
1.0e-04 *
-0.4935 -0.1451 0.2985
Presumably this means that something isn't the value you think it is...