MATLAB Answers

How do I display multiple num2str in a single row

7 views (last 30 days)
Ethan Bales
Ethan Bales on 20 Sep 2021
Commented: Stephen on 20 Sep 2021
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
f = function_handle with value:
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;
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
itn dx xmin xmax f(xmin) f(xmax) xmid f(xmid)
10.0051.851.8550.19038-0.0741521.855-0.074152 20.00251.85251.8550.060334-0.0741521.85250.060334 30.001251.85251.85380.060334-0.00638021.8538-0.0063802 40.0006251.85311.85380.027112-0.00638021.85310.027112 50.00031251.85341.85380.0104-0.00638021.85340.0104 60.000156251.85361.85380.002018-0.00638021.85360.002018 77.8125e-051.85361.85370.002018-0.00217911.8537-0.0021791 83.9062e-051.85361.85360.002018-8.002e-051.8536-8.002e-05 91.9531e-051.85361.85360.00096911-8.002e-051.85360.00096911 109.7656e-061.85361.85360.00044458-8.002e-051.85360.00044458 114.8828e-061.85361.85360.00018229-8.002e-051.85360.00018229 122.4414e-061.85361.85365.1135e-05-8.002e-051.85365.1135e-05 131.2207e-061.85361.85365.1135e-05-1.4442e-051.8536-1.4442e-05 146.1035e-071.85361.85361.8347e-05-1.4442e-051.85361.8347e-05





Community Treasure Hunt

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

Start Hunting!

Translated by