Function, dot, help me for solution

1 vue (au cours des 30 derniers jours)
Gloria
Gloria le 2 Août 2022
Commenté : Benjamin Kraus le 2 Août 2022
% I couldnt find my mistake
v=19*10^-3;
N=314;
R=25*10^-3;
L=50*10^-3;
C=1*10^-4;
e=0.1:0.1:0.9;
W=@(e) v*N*R*L*(L/C)^2*(e./4).*((16*e.^2+pi^2*(1-e.^2))^0.5/(1-e.^2).^2);
%W depends on e
sm=@(W) v*N*L*R/(4*W.*(L/C)^2);
%sm depends on W
plot(e,sm(W))
Operator '*' is not supported for operands of type 'function_handle'.

Error in solution (line 13)
sm=@(W) v*N*L*R/(4*W.*(L/C)^2);
  5 commentaires
Gloria
Gloria le 2 Août 2022
Modifié(e) : Gloria le 2 Août 2022
I guess I am making mistake with dot , the place of the dot is wrong,even this part of the code dose not working
v=19*10^-3;
N=314;
R=25*10^-3;
L=50*10^-3;
C=1*10^-4;
e=0.1:0.1:0.9;
W=@(e) v*N*R*L*(L/C)^2*(e./4).*((16*e.^2+pi^2*(1-e.^2))^0.5/(1-e.^2).^2);
figure
plot(e,W(e))
Walter Roberson
Walter Roberson le 2 Août 2022
W = @(e) v*N*R*L*(L/C)^2*(e./4).*((16*e.^2+pi^2*(1-e.^2))^0.5./(1-e.^2).^2);

Connectez-vous pour commenter.

Réponse acceptée

Benjamin Kraus
Benjamin Kraus le 2 Août 2022
This line has the error:
sm=@(W) v*N*L*R/(4*W.*(L/C)^2);
Specifically: W is an anonymous function, so you can't do 4*W. That is what is generating the error message.
You need to evaluate your function W using some value of e before you can multiply it by 4. Alternatively, don't even create an anonymous function, and instead create W using the value of e from the previous line:
v = 19*10^-3;
N = 314;
R = 25*10^-3;
L = 50*10^-3;
C = 1*10^-4;
e = 0.1:0.1:0.9;
W = v.*N.*R.*L.*(L./C).^2.*(e./4).*((16.*e.^2+pi.^2.*(1-e.^2)).^0.5./(1-e.^2).^2);
sm = v.*N.*L.*R./(4.*W.*(L./C).^2);
plot(e,sm);
  2 commentaires
Gloria
Gloria le 2 Août 2022
Thank you so much!
I have another question. What if I also want to define 3 different L, I want to have three different line on the graph for L*1, L*2 and L*3.
How can I write function for this?
Benjamin Kraus
Benjamin Kraus le 2 Août 2022
If you have three different values for L, you can either create three instance of W and three instances of sm (i.e. W1, W2, etc.), or you can define W and sm as anonymous functions that take L as an input parameter, as @John D'Errico suggested in their answer.
Alternatively, you can leverage implicit expansion by defining L as a column vector and e as a row vector (or vice versa), as described in this blog post, but that's a bit of an advanced manuever.

Connectez-vous pour commenter.

Plus de réponses (1)

John D'Errico
John D'Errico le 2 Août 2022
Modifié(e) : John D'Errico le 2 Août 2022
Your problem has NOTHING to do with dots. This is a common mistake we see made here.
Suppose you have a pair of function handles, and you want to "add" the functions together? For example, suppose we try this:
F1 = @(x) x.^2;
F2 = @(x) x+1;
One might hope that the sum of those two function handles would be just a new function handle. I might try to write it as F3 = F1 + F2. If I did however, MATLAB would throw an error, just as you found. Instead, you need to create a new function handle, by adding the results of each of the two.
F3 = @(x) F1(x) + F2(x);
As you can see, we can use it as we would expect to happen.
F3(5)
ans = 31
Had I just tried to add or multiply the two function handles together, we would an error as you saw:
F1 * F2
Operator '*' is not supported for operands of type 'function_handle'.
As I said, you CANNOT add, multiply, subtract or divide function handles. Arithmetic operations do not apply to function handles. You need to do it as I showed above.
  1 commentaire
Gloria
Gloria le 2 Août 2022
Thank you so much!

Connectez-vous pour commenter.

Catégories

En savoir plus sur File Operations dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by