Function only computing first element of array used as input

조회 수: 3 (최근 30일)
Carsten
Carsten 2023년 3월 23일
댓글: Carsten 2023년 3월 23일
I am trying to use a MATLAB function to generate a plot for the function :
As such I am using the linspace function to generate an input array that I then aim to give to the function i wrote to get data to be plotted. But the function only processes the first element of the Array generated by linspace. The function appears to work correctly for singular values but not for arrays.
function S = FXSUM(x)
Tot = 0; % reset Total
for n = 2:x % for loop for Function Summs at a iterations
it = (1/(n*((log(n))^2))); % provided function
Tot = Tot + it % sum of function iterations
end
S = Tot; % Out value equal to function iterrations total
end

채택된 답변

Cris LaPierre
Cris LaPierre 2023년 3월 23일
이동: Cris LaPierre 2023년 3월 23일
Your function is not written to accept a vector as input. You likely need to add a second loop to run for each element of your input vector.
Right now, it creates your loop counter using 2 and the first element of x.
x = 5:8
x = 1×4
5 6 7 8
2:x
ans = 1×4
2 3 4 5
  댓글 수: 1
Carsten
Carsten 2023년 3월 23일
Thank you very much. Still getting used to MATLAB and its behaviour. Similar function worked fine on a previous calculation, though i did not have a loop set to run to a variable end point, which seemed to be the issue here when said variable is a vector.
Issue was successfully resolved using a second for loop to read points of the vector, process with the above loop and then feed back into the solution vector.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by