Placing the output of a power fit function to the array

4 views (last 30 days)
Dear All,
I have a problem on a power fit function. Here is my code;
A = [90;200;300;1220];
B = [370;470;500;530]
%fit those points on a power function
f = fit(A,B,'power2')
%until now everythings ok. I got the function but here is the problem
B2 = zeros(1,length(A2)) %create 1xlength(A2) matrix
m = 1;
%for loop #1
for i = Amin:Astep:Amax
B2(m) = f(i);
m = m+1;
end
%I want this for loop assign all f(i) functions to the B2 matrix where Amin = 90 , Amax = 100 and Astep = 1
m = 1;
%for loop #2
for i = 90:1:100
B2(m) = f(i);
m = m+1;
end
Here is the problem. In for loop #1, I got "Warning: Power functions require x to be positive. Non-positive data are treated as NaN" and all outputs are NaN.
However, in for loop #2, all are the same but 'i' counter. I used direct integers instead of Amin,Amax and Astep and I got exactly what I want.
What is wrong with the for loop #1? How can I obtain a reasonable results by using Amin,Amax,Astep?
Thanks in advanced

Answers (1)

Himanshu
Himanshu on 28 Oct 2024 at 9:20
Hey Merih,
I can identify two main issues here. The first is that you have not defined Amin Amax and Astep anywhere in the code. Another issue is that you have not initialized matrix B2 to the correct size (I am not sure what A2 represents). Fixing these two issues, as shown in the following code, should help you get rid of the warning.
A = [90; 200; 300; 1220];
B = [370; 470; 500; 530];
% Fit the points to a power function
f = fit(A, B, 'power2');
% Define Amin, Amax, and Astep
Amin = 90;
Amax = 100;
Astep = 1;
% Create a matrix for results
B2 = zeros(1, length(Amin:Astep:Amax));
% Initialize the index
m = 1;
% For loop #1
for i = Amin:Astep:Amax
B2(m) = f(i);
m = m + 1;
end
% Display the results
disp(B2);
Hope this helps!

Community Treasure Hunt

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

Start Hunting!