I just need this one TINY code to execute :( Any idea what the problem with this simple equation is?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
KLETECH MOTORSPORTS
el 8 de Nov. de 2020
Editada: Walter Roberson
el 15 de Nov. de 2020
I need to put this Either in another function or as a seperate function. Or the script. It's accepting L, M, and b values, but i am not getting an output value for y.
suggestions?
prompt1 = 'Enter value of L';
prompt2 = 'Enter value of M';
prompt3 = 'Enter value of b';
L = input(prompt1)
M = input(prompt2)
b = input(prompt3)
y = (12*9.81*L)/(4*L^.2)+(b.^2)-(12*L*M)+(12*M.^2);
0 comentarios
Respuesta aceptada
Ameer Hamza
el 8 de Nov. de 2020
Editada: Ameer Hamza
el 15 de Nov. de 2020
You need to print the output. For example using disp()
prompt1 = 'Enter value of L';
prompt2 = 'Enter value of M';
prompt3 = 'Enter value of b';
L = input(prompt1)
M = input(prompt2)
b = input(prompt3)
y = (12*9.81*L)/(4*L^.2)+(b.^2)-(12*L*M)+(12*M.^2);
disp(y)
Or remove the semicolon from the last line
y = (12*9.81*L)/(4*L^.2)+(b.^2)-(12*L*M)+(12*M.^2)
%^ removed semicolon from here
8 comentarios
Walter Roberson
el 8 de Nov. de 2020
Editada: Walter Roberson
el 15 de Nov. de 2020
A scalar is any array that holds exactly one piece of information -- not empty, and not two or more values.
The operators * and ^ and / and \ are defined as being matrix operations:
- A*B is algebraic matrix multiplication, inner product of A and B . For A*B to be valid, size(A,2) must be the same as size(B,1) and they cannot have 3 or more dimensions
- A^B is matrix power; when B is a positive integer it is like A*A*A*A...*A for a total of B times, where * here is algebraic matrix multiplication (inner product); the same size restrictions exist as for *
- A/B is like A * pinv(B) but not exactly the same . It is a least-squared operation suitable for solving linear equations. A\B is like pinv(A)*B
These differ from the .* and .^ and ./ and the (rare) .\ operators, all of which are element-by-element operations. C = A.*B is C(J,K) = A(J,K) .* B(J,K) -- multiplication of corresponding locations; likewise for the other operations mentioned.
The matrix operator * acts like .* if one of the operands is scalar instead of matrice. 3*A is A with each element multiplied by 3. The ^ and / operators act like .^ and ./ if both of the operands are scalar.
You have written (12*9.81*L)/((4*L^.2)+(b.^2)-(12*L*M)+(12*M.^2)) . If L happens to be something that is not a scalar, then almost everything you wrote there will work -- the L.^2 would work. For example [2 5]^2 would fail because it would be [2 5]*[2 5] and size() of the second dimension of the first operand is 2 but size() of the first dimension of the second operand is 1 which does not match. [2 5]^2 is not [4 25]. But [2 5].^2 is [4 25] . You wrote your expression nearly all with the dot operators, so nearly all of it will work if L or M happen to be matrices. But you used / and / between two things that are not scalar is going to give you results that you do not expect. You need ./ in that expression if there is a chance that L is not a scalar. If L and M are both something that might not be scalar then L*M should be L.*M to get your code to work the way you expect.
Más respuestas (0)
Ver también
Categorías
Más información sobre Function Creation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!