How to display value of particular iteration in a loop

I am having a slight problem, I'd like to find a relative error without knowing the analytical solution. I have an equation where i need to find the value of F using composite mid rule when the relative error is less than 1%. Here is my code so far: The equation is F= integral (from 0 to 30) of (200*(z(n)/(5+z(n)))*exp((-2*z(n))/30))dz :
clc
clear all
a=0;
b=30;
s=1000;
dx=(b-a)/s;
z=zeros(1,s);
for n=1:s
z(n)=a+dx/2+(n-1)*dx;
F=0;
for n=1:s
F=F+dx*(200*(z(n)/(5+z(n)))*exp((-2*z(n))/30));
end
sprintf('%6.2f',F)

6 件のコメント

Jan
Jan 2018 年 8 月 14 日
You forgot to ask a question. What is the problem with this code?
Piotr Haciuk
Piotr Haciuk 2018 年 8 月 14 日
Finding solution of F using composite mid point rule with relative error less than 1%
Piotr Haciuk
Piotr Haciuk 2018 年 8 月 14 日
here is the task: In a model of a racing sailboat, the total force, F, on the mast is expressed in the integral form - F= integral (from 0 to 30) of (200*(z(n)/(5+z(n)))*exp((-2*z(n))/30))dz Use the composite mid-point rule to approximate the value of F with a relative error of less than 1%. Assume that the analytic solution is not known when calculating the relative error. answers: 100 intervals: 1480.72; 200 intervals: 1480.61. Relative error much less than 1%]
Matt J
Matt J 2018 年 8 月 14 日
編集済み: Matt J 2018 年 8 月 14 日
But you have code already. What is left to do? If the code is not working as you wish, in what way can we see that?
Piotr Haciuk
Piotr Haciuk 2018 年 8 月 14 日
How can i find the relative error? the answers i wrote i should expect. When you change the value of s for 100 i get the answer 1480.72, for 200 i get the answer 1480.61, for s=1000 i get 1480.57, but when running it with s=1000 when i want to take the value for n=100 it doesnt give me same answer as for s=100
Piotr Haciuk
Piotr Haciuk 2018 年 8 月 14 日
Also, at which iteration the relative error of the si=olution is less than 1%

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

回答 (1 件)

Alberto Mora
Alberto Mora 2018 年 8 月 14 日

0 投票

Maybe you can use this piece of code inside the for loop:
if n==desired_iteration
disp('This is the desired iteration!');
disp(['The result at ',num2str(n),' iteration is ',num2str(result)]);
end

6 件のコメント

Piotr Haciuk
Piotr Haciuk 2018 年 8 月 14 日
How to i implement it to a loop?
Alberto Mora
Alberto Mora 2018 年 8 月 14 日
if true
% code
endclc
clear all
a=0;
b=30;
s=1000;
dx=(b-a)/s;
z=zeros(1,s);
for n=1:s
z(n)=a+dx/2+(n-1)*dx;
F=0;
for n=1:s
F=F+dx*(200*(z(n)/(5+z(n)))*exp((-2*z(n))/30));
if n==desired_iteration
disp('This is the desired iteration!');
disp(['The result at ',num2str(n),' iteration is ',num2str(result)]);
end
end
end
sprintf('%6.2f',F)
Obviously you need to use your variable instead of "desired_iteration", "result".
Piotr Haciuk
Piotr Haciuk 2018 年 8 月 14 日
Okay what if i want to know at which iteration the relative arror is less than 1%?
Torsten
Torsten 2018 年 8 月 14 日
Choose n such that
K*(b-a)^3/(24*n^2) < 0.01
where
K = max_[x in [a;b]] |f''(x)|
Best wishes
Torsten.
Piotr Haciuk
Piotr Haciuk 2018 年 8 月 14 日
Torsten, how to implement it to my code?
Torsten
Torsten 2018 年 8 月 14 日
As you can see from the answer provided in your textbook, the authors seem to define the relative error as
|F_(2*n)-F_n|/|F_n|
So simply calculate F for n and 2*n and evaluate the expression above. If it is less than 0.01, you are done. Otherwise you will have to increase n and repeat the procedure.

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

カテゴリ

ヘルプ センター および File Exchange で Loops and Conditional Statements についてさらに検索

製品

リリース

R2017a

質問済み:

2018 年 8 月 14 日

編集済み:

2018 年 8 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by