invalid use of operator

>>shitathatsia = @(x) 2.7*cos(25*x^2+8*x)-10*x*sin(6*x)
starrt=-1
ennd=1
pitaron= (starrt+ennd)/2
while sqrt((shitathatsia(pitaron))^2)> 0.001
if(shitathatsia(pitaron)) * shitathatsia(start) <0
starrt = pitaron
if shitathatsia(pitaron)) * shitathatsia(start) >0
ennd=pitaron
end
pitaron=(starrt+ennd)/2
end
fprintf(pitaron)

Answers (2)

You are missing a ( after the second "if", and you are missing an "end" statement. That being said, my suggestion would be to use an "else" instead as that appears to be your intent. E.g.,
if ( condition )
starrt = pitaron;
else
ennd = pitaron;
end

8 Comments

but i get an error in first line
invalid use of operator
James Tursa
James Tursa on 3 May 2021
Edited: James Tursa on 3 May 2021
Please show your current code with your attempted fixes. And please post your code as text highlighted with the CODE button, not as images. We can't copy & run images at our end.
>>shitathatsia = @(x) 2.7*cos(25*x^2+8*x)-10*x*sin(6*x)
starrt=-1
ennd=1
pitaron= (starrt+ennd)/2
while sqrt((shitathatsia(pitaron))^2)> 0.001
if(shitathatsia(pitaron)) * shitathatsia(start) <0
starrt = pitaron
if shitathatsia(pitaron)) * shitathatsia(start) >0
ennd=pitaron
end
pitaron=(starrt+ennd)/2
end
fprintf(pitaron)
The >> should not be part of your code
This looks like your original code. Why haven't you implemented the fix I suggested? Also, you should be using the variable name starrt instead of start. E.g.,
if(shitathatsia(pitaron)) * shitathatsia(starrt) <0
starrt = pitaron;
else
ennd = pitaron;
end
And it seems like this expression
sqrt((shitathatsia(pitaron))^2)
could simply be replaced with this
abs(shitathatsia(pitaron))
i have to error from the first line my friend
File: untitled.m Line: 1 Column: 1
Invalid use of operator.
Get rid of the >> as Walter suggests.

Sign in to comment.

for i=1:length(t)-1
x(i+1)=x(i)+(a*(x^2)(i)*(1+B(x^2)(i)-q*x(i)*E(i)))*Dt;
E(i+1)=E(i)+(z*(p*q*x(i)*E(i)-c*E(i)))*Dt;
end;
I am getting invalid use of operator with the above syntex, pls how do i resolve it. thank you.

4 Comments

(x^2)(i)
that would be considered a request to apply a matrix square to x and then index the result at i.
This has two problems:
  • matlab does not permit () indexing of the results of a calculation
  • your x is a vector but you are taking matrix square, which can only be applied to square matrices, not to vectors.
x^2 is x*x which is inner product of x with itself, also known as matrix multiplication.
Perhaps in your situation it would be acceptable to use x(i).^2
Thanks so much for the response. I just tried that option but I'm still getting the same error message
% Euler's Method
% Initial conditions and setup
% x(t+1)=x(t)+(a*(x(t)^2)*(1+B(x(t)^2)-q*x(t)*E(t)))*Dt
% E(t+1)=E(t)+(z*(p*q*x(t)*E(t)-c*E(t)))*Dt
%
%=======================================================================
a=1; alpha value
B=25; beta value
q=0.1; %catchability
z=0.1; %effort
p=100; %price
c=4; %cost
Dt=0.01; %delta t
timesteps=50;
t=0.1:Dt:timesteps; %array with time
for i=1:length(t)-1
x(i+1)=x(i)+(a*(x(i)^2)*(1+B(x(i)^2)-q*x(i)*E(i)))*Dt;
E(i+1)=E(i)+(z*(p*q*x(i)*E(i)-c*E(i)))*Dt;
end;
B(x(i)^2) is a request to index your scalar value B at the location determined by the calculation. Perhaps you forgot a multiplication.

Sign in to comment.

Asked:

on 3 May 2021

Commented:

on 27 Mar 2022

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!