Update values in while loop and store values from a while loop in arrays

16 ビュー (過去 30 日間)
Nnamdi Chukwunenye
Nnamdi Chukwunenye 2020 年 9 月 3 日
コメント済み: Neeraj Kumar 2020 年 9 月 16 日
Hi I am trying to run a while loop to calculate the error values. I am struggling to update the values in the while loop and then store them all. I need to store the number of iterations and all the error values per iteration while update the value of x_old to call my function again. I have my code posted below, but I can't get my values to update. Any helo would be appreciated.
% Exercise 2.1 Order of Convergence
clear
close
clc
% Define Variables
F = 12;
x_old = 0;
itr = 0;
epsilon_new = 1e30;
epsilon_target = 1e-6;
while epsilon_new > epsilon_target
itr = itr+1;
[x_new] = springcalc(F, x_old);
epsilon_new = abs(x_new - x_old);
eps_matrix = [epsilon_new];
x_old = x_new;
end
semilogy(itr, epsilon_matrix);
function [x_new] = springcalc(F, x_old)
k_0 = 30;
x_new = F/(k_0*(1+x_old^(0.1)));
end

採用された回答

David Hill
David Hill 2020 年 9 月 3 日
編集済み: David Hill 2020 年 9 月 3 日
% Exercise 2.1 Order of Convergence
clear
close
clc
% Define Variables
F = 12;
x_old = 0;
itr = 0;
epsilon_new = 1e30;
epsilon_target = 1e-6;
while epsilon_new > epsilon_target
itr = itr+1;
x_new = springcalc(F, x_old);
epsilon_new = abs(x_new - x_old);
epsilon(itr)=epsilon_new;
x_old = x_new;
end
semilogy(1:length(epsilon), epsilon);
function [x_new] = springcalc(F, x_old)
k_0 = 30;
x_new = F/(k_0*(1+x_old^(0.1)));
end
  3 件のコメント
David Hill
David Hill 2020 年 9 月 3 日
Sorry, fixed.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by