Returning last value of while loop

I need to return the last integral of the while loop, the one that is small enough to satisfy the inequality, so that the function value (q) is equal to H with the last value of k. How can I do that?
function q = myimproperintegral(f,a,b,tol)
syms x;
k = 2;
H=0;
before = int(f(x),(a+(b-a)/k),b);
after = int(f(x),(a + (b-a)/(k+1)),b);
while before-after > tol
k=k+1;
H=int(f(x),(a+(b-a)/(k+1)),b);
end
q=double(H)
end

답변 (1개)

Jos (10584)
Jos (10584) 2015년 3월 10일

0 개 추천

You can return multiple outputs with functions
function [q, k] = myimproperintegral(f,a,b,tol)
...

댓글 수: 1

chazz
chazz 2015년 3월 10일
I only need one output, but the code above doesn't work for what I'm trying to do. All I need is for q to equal H with the last value of k...

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

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

질문:

2015년 3월 10일

댓글:

2015년 3월 10일

Community Treasure Hunt

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

Start Hunting!

Translated by