how to do iteration starting with a known number

5 views (last 30 days)
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
  2 Comments
mohamed
mohamed on 31 Mar 2016
ok so far i got the first value but i dont know how to tell matlab to substitute with the new value of pws again

Sign in to comment.

Answers (1)

Swastik Sarkar
Swastik Sarkar on 29 May 2025
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.

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!