Using the function fzero in a for loop
1 次查看(过去 30 天)
显示 更早的评论
I have three functions
fn1 = At^2 + Bt + C
fn2 = Mt + N
fn3 = Zt + W
where the coefficients [A,B,C,M,N,Z,W] correspond to columns [1,2,3,4,5,6,7] of the following data set
12 4 8 7 1 1 2
9 2 2 1 5 5 1
2 7 9 6 6 5 6
each row represents a set of coefficients.
My goal is to find the x values and y values for every intersection point for each set of coefficients.
So far, I have found out how to find the intersecting values for only one set, but I am not sure how to use a for loop when using fzero. Below is what I have for one row and my attempt at implementing a for loop.
for i = 1:1:r
int_1(i) = Fn1(i) - Fn2(i);
int_2(i) = Fn1(i) - Fn3(i);
end
X1 = fzero(int_one,0);
I used the line above to find the x values
Y1 = F2(X1);
I used the line above to find its corresponding y values.
This works.
Here, r is the number of rows in the matrix, in my case, r is 20. This works. it subtracts the functions at every iteration and stores the results correctly in int_1 and int_2. Then, I use the function fzero to find its corresponding y values closest to the value 0 and closest to the value 3. 0 and 3 are the starting and ending values on my x axis. However, the following:
for ii = 1:1:r
X1(ii) = fzero(int_one(ii),0);
X2(ii) = fzero(int_two(ii),0);
X3(ii) = fzero(int_two(ii),3);
X4(ii) = fzero(int_one(ii),3);
Y1(ii) = F2(X1(ii));
Y2(ii) = F3(X2(ii));
Y3(ii) = F3(X4(ii));
Y4(ii) = F2(X3(ii));
end
The code above does not work, and I am not sure why. I have tried
X1(ii) = fzero(@(m)int_one(m,ii),0); for the x values but this also does not work.
If someone could guide me I would greatly appreciate it.
0 个评论
回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!