How do I display multiple num2str in a single row
7 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hello, I'm having some issues with my disp function in my while loop. I'm trying to display my values accordingly to the disp function underneath my if statement, where I show how the according values change through each iteration of the while loop. I think the way I coded it is making it so that it adds all the values together rather than displaying the functions in separate columns. Could I have some help with this?
n = 2;
m = 1;
f = @(x) (0.4*sin(2*pi*x.^n)).*(exp(x.^m))-1
a = 1.85; %Boundary 1
b = 1.86; %Boundary 2
err = 1; %Had to assign error a random value just so that it exists
counter = 0; %Keeps track of the iterations
if f(a)*f(b)<0 %Product of f(a) and f(b) guarantees a root between the boundaries
disp("itn dx xmin xmax f(xmin) f(xmax) xmid f(xmid)")
while err > 0.000001 %The while loop will keep looping until our error dips below 10^-6
t = (a+b)/2; %t will be our midpoint
if f(t)*f(a)<0 %If the product of f(t) and f(a) is negative, we make b equal to t
b = t;
elseif f(t)*f(b)<0 %if the product of f(b) and f(t) is negative, we make a equal to t
a = t;
end
counter = counter + 1; %Updates each time we go through the while loop
disp([num2str(counter) num2str(b-a) num2str(a) num2str(b) num2str(f(a)) num2str(f(b)) num2str(t) num2str(f(t))])
err = abs(b-a); %Absolute value prevents negative values from ending the while loop
end
end
0 comentarios
Respuestas (1)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!