problem in functin (y for x)

3 visualizaciones (últimos 30 días)
Lila wagou
Lila wagou el 1 de Mzo. de 2017
Editada: Stephen23 el 1 de Mzo. de 2017
Hy i have a problem in the flowing code (XZData.m function)
function [x,y] = XZData(A,B,C)
x = 0:1:10;
y = (A*x^2)+(B*x)+C;
XY = ['XData', num2str(x), 'yData', num2str(y)];
disp(XY)

Respuesta aceptada

Star Strider
Star Strider el 1 de Mzo. de 2017
Editada: Stephen23 el 1 de Mzo. de 2017
Use element-wise exponentiation in the ‘y’ assignment:
y = (A*x.^2)+(B*x)+C;
↑ ← INSERT ‘.’ HERE
function [x,y] = XZData(A,B,C)
x = 0:1:10;
y = (A*x.^2)+(B*x)+C;
XY = ['XData ', num2str(x), ' yData ', num2str(y)];
disp(XY)
end
See the documentation on Array vs. Matrix Operations for details.

Más respuestas (2)

Walter Roberson
Walter Roberson el 1 de Mzo. de 2017
Use x.^2 with the dot instead of x^2

John D'Errico
John D'Errico el 1 de Mzo. de 2017
Editada: John D'Errico el 1 de Mzo. de 2017
It makes it easier to get help if you report the (COMPLETE) error message that you got. Help those who would help you.
In this case though, the problem is that you need to learn about the elementwise operators, like .* ./ and .^ , since that is where your immediate problem arises.
Change the computation of y to this:
y = (A*x.^2)+(B*x)+C;
x is a vector. You wish to compute the square of each element of x, so use the .^ operator.
Admittedly, I think your next problem is that you won't like how that line gets displayed in the call to disp. It will be one long line of text. But that is for you to decide.

Categorías

Más información sobre Logical en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by