Have problem in solution

1 view (last 30 days)
Ratchapon Nilprapa
Ratchapon Nilprapa on 23 Sep 2021
Commented: Rik on 26 Sep 2021
I'm new to Matlab and I have a problem with this solution on line 14. Please give me for any advice. Thank you
  2 Comments
John D'Errico
John D'Errico on 23 Sep 2021
When you post code, post it as text, not a picture of text. Otherwise, you force someone to type in your entire code, unless they can see an obvious error. If you want help, then make it easy for someone to help you.
Ratchapon Nilprapa
Ratchapon Nilprapa on 26 Sep 2021
My apologize, thank you for your advise Mr.John D'Errico.

Sign in to comment.

Accepted Answer

Rik
Rik on 23 Sep 2021
Edited: Rik on 23 Sep 2021
It looks like you want different things to happen depending on the value of Erf: for every element smaller than 2 you want y to have some value, and for other elements you want a different calculation.
But that is not what you tell Matlab to do. You see all those orange squiggly lines? Those are warnings. You should read them and deal with them. In this case I suspect this is the solution: use a for-loop.
Erf=[0.2 0.6 0.7333];
y=NaN(size(Erf));
for n=1:numel(Erf)
if Erf(n)<2
y(n)=(-0.3725*Erf(n)^2);%and the rest the of the line
else
y(n)=(-0.0109*Erf(n)^2);%and the rest the of the line
end
end
You can also do this in one go with logical indexing, in which case you need to use element-wise operations (.^ instead of ^, and similarly for multiplication and division).
Erf=[0.2 0.6 0.7333];
y=NaN(size(Erf));
L=Erf<2;
y(L)=-0.3725*Erf(L).^2;
L=~L;
y(L)=-0.0109*Erf(L).^2;
These two blocks of code yield the same result.
  2 Comments
Ratchapon Nilprapa
Ratchapon Nilprapa on 26 Sep 2021
Thanks for your kindness that you explain to me clearly, Rik.
Rik
Rik on 26 Sep 2021
You're welcome. If either solution solved your issue, please click the 'accept answer' button. If you feel the other answer helped as well, feel free to click the vote button.
If neither solved your problem, feel free to comment with your remaining issues.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 23 Sep 2021
Edited: Walter Roberson on 23 Sep 2021
Erf=[0.2 0.6 0.7333];
Erf is a vector.
y=(-0.3725*(Erf)^2)+(1.2144*(Erf))+(0.0006);
You have Erf^2 . But in MATLAB, the ^ operator is repeated matrix multiplication -- so Erf^2 is (Erf*Erf) where * is the algebraic matrix multiplication operator, also known as Inner Product. For Inner Product A*B, the number of columns of A (the first operand) must be the same as the number of rows of B (the second operand) . You hae a 1 x 3 vector, so you effectively have (1 x 3) * (1 x 3), but the number of columns in the first operand, 3, does not match the number of rows in the second (1).
You probably want the element-by-element power operator, which in MATLAB is the .^ operator
y=(-0.3725*(Erf).^2)+(1.2144*(Erf))+(0.0006);
Be careful: your line 19 completely overwrites y, overwriting the value assigned to y in line 18.
Note: it is easier for the volunteers to assist you if you post code, instead of posting pictures of code. Don't make the volunteers type out the code by hand in order to test it or point out which parts of it have difficulties.
  1 Comment
Ratchapon Nilprapa
Ratchapon Nilprapa on 26 Sep 2021
Thanks for your kindness that you explain to me clearly, Walter Roberson.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!