MATLAB Answers

how can i get all i for loop. the output only give me the last iterations. below are my coding.

1 view (last 30 days)
Nurain Nadhirah
Nurain Nadhirah on 6 Oct 2021
Answered: Image Analyst on 7 Oct 2021
case 2 %newton
d = (get(handles.enterfunction,'string'));
syms x;
f = inline(d);
z = diff(f(x));
f1 = inline(z);
x0= str2num(get(handles.initialvalue,'string'));
tol = str2double(get(handles.tolerance,'string'));
max = str2double(get(handles.max,'string'));
for u=0:10000
y=x;
x=y-[f(x)/f1(x)];
if abs((y-x)/x) < tol
x=y;
break
end
end
x = num2str(x);
u = num2str(u);
display = [ u x f(x)];
set(handles.uitable5,'data',display);
set(handles.roots,'string',x);
set(handles.iterations,'string',u);
drawnow()
  3 Comments
Rik
Rik on 7 Oct 2021
If you want to add elements to a table, you will have to add them. What you're currently doing is overwriting all your variables with the new values. Which exact values do you want to save?

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 7 Oct 2021
There is no "i" in the code that I noticed. Which is good because we don't recommend using i as a variable since i is the imaginary constant sqrt(-1).
But in general to save a variable into an array you need to index it with the loop iterator value.
for k = 1 : 10
myVariable(k) = rand(1); % Or whatever. Notice the (k) which will make myVariable a vector.
end

Community Treasure Hunt

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

Start Hunting!

Translated by