- starting value
- ending value
- total number of values to use. In the below discussion I will refer to this as n
How to use linspec
10 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Ann Little
am 4 Dez. 2019
Beantwortet: Walter Roberson
am 4 Dez. 2019
given the question: Determine z = 2x^3 + 3y^2 + 5, when x and y vary from -4 to 4 with a spacing of 1.
solution: x = linspace(-4,4,9);
y = linspace(-4,4,9);
z = 2*x^3+3*y^2+5
Question: why is the "n" value for the linspace of x and y = 9 and not 1?
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 4 Dez. 2019
The parameters of linspace are
The output values are constructed so that the starting value and ending value are used exactly, and the (n-2) intermediate values are spaced approximately (ending - starting)/(n-1) apart .
So with -4, -4, 9, the (9-2)=7 intermediate values are spaced (4-(-4))/(9-1) = 8/8 = 1 apart, so -4 (used exactly), then -4+1*1 = -3, -4+1*2 = -2, -4+1*3 = -1, -4+1*4 = 0, -4+1*5 = +1, -4+1*6 = +2, -4+1*7 = +3, then +4 (used exactly).
This is not exactly the code that is used; the exact code has some subtle differences in the last few bits due to floating point round-off in how it is really calculated.
0 Kommentare
Weitere Antworten (1)
galaxy
am 4 Dez. 2019
n = 9 when user want to generate integer cases of x in [-4, 4]
if n = 1, so x alway = 4, it is no meaning
0 Kommentare
Siehe auch
Kategorien
Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!