how to do iteration starting with a known number
5 次查看(过去 30 天)
显示 更早的评论
hello i need to do an iteration process to obtain (pws) i have several variables that pws depend on and i need to know how to solve the problem here is a screenshot for the problem
<<

>> and all elements in the question are present like pts,tts,l and theta
thank you
回答(1 个)
Swastik Sarkar
2025-5-29
Hi Mohammed,
I believe this can be approached in two ways depending on whether you want to manually iterate or use MATLAB's Optimization Toolbox.
The Manual implementation of the iteration logic in MATLAB is like this:
pws1 = pts + 0.25 * (pts/100) * (L * cos(theta))/100;
currentIteration = 0;
maxIterations = 100 % Some limit as the solution might not converge
while currentIteration < maxIterations:
Pavg = (pws1 + pts)/2; % pws1 is consumed here
% do the other calculations
% assign the new value of pws1
pws1 = newlyCalculatedValue
if(conditionMet):
break
end
end
Alternatively, if you're solving for pws such that it satisfies a fixed-point condition like pws = f(pws), you can use fsolve:
% Define the function to find the fixed point
f = @(pws) pws - someFunction(pws); % Goal: pws = someFunction(pws)
pws1 = pts + 0.25 * (pts/100) * (L * cos(theta))/100;
% Solve using fsolve
options = optimoptions('fsolve', 'Display', 'iter');
[pws_solution, fval, exitflag] = fsolve(f, pws1, options);
This is more elegant if your iteration is actually trying to solve an equation implicitly.
For more information on fsolve, refer the following MathWorks Documentation:
Hope this helps converge to the solution.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!