Plotting data within while loop,help

No plot displays even though the function works, at the end of the function
use this in command window
w = 25.5; E = 50*10^3;I = 30*10^3; L = 600;
dy = @(x) (w/(120*E*I*L))*((-5*x^4)+(6*L^2*x^2)-(L^4))
[xmax fx eapprox iteration]=bisect(dy,0.1,600,2)
%writing function for Bisection Method
function [root,fx,eapprox,iteration]= bisect(funct, xlower, xupper, deserr)
iteration = 0; xr = xlower; eapprox = 100;
%m = mass = 80 kg
% v = velocity,36m/s
% g = gravitational acceleration = 9.81 m/s^2
%x1 = lower guess (given 0.1)
%xu = upper guess (given 0.2)
% t = time = 4s
%root = root finding (cd, mass, time, etc)
%eap = approximate relative error
% es = desired relative error, <=2%
%fx = function value at root
hold on
while (1)
xold = xr; % previous xr value for comparision for approximate relative error
xr = (xlower +xupper)/2; % solve mean for interval
iteration = iteration +1; % increment for iterations
if xr ~= 0 % if mean doesnt equal 0
eapprox = abs(xr -xold / xr)*100; % solving approximate relative error for xl and xu
end %end of first if statement
test = funct(x1ower)*funct(xr); %testing to see if interval needs to be moved left or right
if test > 0 % positive
xlower = xr;
else
if test < 0 %negative
xupper = xr;
else
eapprox = 0; % if there is 0 approximate error that means the root is found
end %end of second if statement
if eapprox <= deser
break,end %end of if statements
end %end of while loop
%disp(output)
plot(iteration,xr,'ro')
plot(iteration,eapprox, 'b')
root = xr,
end %end of fucntion

5 commentaires

Bob Thompson
Bob Thompson le 29 Jan 2019
There seem to be a number of issues with this code, even before getting to the lack of plots. For example you have x1ower instead of xlower, and deser is undefined. Could you please double check that your copy here is correct?
Alexa Shumaker
Alexa Shumaker le 29 Jan 2019
I am not good at coding to begin with. xlower is fixed. Not changing deser i its required to keep it like that.
I really just need help on plotting in while loop.
function [root,fx,eapprox,iteration]= bisect(funct, xlower, xupper, deserr)
iteration = 0; xr = xlower; eapprox = 100;
%m = mass = 80 kg
% v = velocity,36m/s
% g = gravitational acceleration = 9.81 m/s^2
%xl = lower guess (given 0.1)
%xu = upper guess (given 0.2)
% t = time = 4s
%root = root finding (cd, mass, time, etc)
%eap = approximate relative error
% es = desired relative error, <=2%
%fx = function value at root
hold on
while (1)
xold = xr; % previous xr value for comparision for approximate relative error
xr = (xlower +xupper)/2; % solve mean for interval
iteration = iteration +1; % increment for iterations
if xr ~= 0 % if mean doesnt equal 0
eapprox = abs(xr -xold / xr)*100; % solving approximate relative error for xl and xu
end %end of first if statement
test = funct(xlower)*funct(xr); %testing to see if interval needs to be moved left or right
if test > 0 % positive
xlower = xr;
else
if test < 0 %negative
xupper = xr;
else
eapprox = 0; % if there is 0 approximate error that means the root is found
end %end of second if statement
if eapprox <= deser
break,end %end of if statements
end %end of while loop
%disp(output)
plot(iteration,xr,'ro')
plot(iteration,eapprox, 'b')
root = xr,
end %end of fucntion
Bob Thompson
Bob Thompson le 29 Jan 2019
Modifié(e) : Bob Thompson le 29 Jan 2019
Ah, I see. deser should be deserr with two 'r's.
Does the loop break for you? When I ran with the inputs you posted I reached an iteration of ~25000 before I aborted because the eapprox and root converged at 2.6733e4 and ~268.32 respectively.
The plotting appears to work fine though, though it was effectively two straight lines, mostly due to the scale.
If you're looking to do plots like this I generally prefer using indexing within the loop, and then a single plot command outside, as it allows for more options in plot configurations. It would look something like this:
iteration = 1;
x = xlower
while (1)
xold = x(iteration);
iteration = iteration + 1;
x(iteration) = mean([xlower xupper]);
% Replace all xr with x(iteration)
...
% Also index eapprox with eapprox(iteration)
end
figure(1)
plot([1:iteration],x,'ro');
figure(2)
plot([1:iteration],eapprox,'b');
Alexa Shumaker
Alexa Shumaker le 29 Jan 2019
this is the input and output I get when I run it
>> w = 25.5; E = 50*10^3;I = 30*10^3; L = 600;
dy = @(x) (w/(120*E*I*L))*((-5*x^4)+(6*L^2*x^2)-(L^4))
[xmax fx eapprox iteration]=bisect(dy,0.1,600,2)
dy =
function_handle with value:
@(x)(w/(120*E*I*L))*((-5*x^4)+(6*L^2*x^2)-(L^4))
xmax =
267.2430
fx =
-1.9801e-04
eapprox =
1.7537
iteration =
7
Bob Thompson
Bob Thompson le 29 Jan 2019
Modifié(e) : Bob Thompson le 30 Jan 2019
There is an order of operations error in the calculation of eapprox.
eapprox = abs(xr -xold / xr)*100; % is in the posting
eapprox = abs((xr -xold) / xr)*100; % Should be
See my edits to my previous comment for notes about plotting.

Connectez-vous pour commenter.

Réponses (2)

Harshit Jain
Harshit Jain le 7 Fév 2019
You can use "drawnow" if you want to plot data in each iteration of while loop.
w = 25.5; E = 50*10^3;I = 30*10^3; L = 600;
dy = @(x) (w/(120*E*I*L))*((-5*x^4)+(6*L^2*x^2)-(L^4))
[xmax fx eapprox iteration]=bisect(dy,0.1,600,2)
%writing function for Bisection Method
function [root,fx,eapprox,iteration]= bisect(funct, xlower, xupper, deserr)
iteration = 0; xr = xlower; eapprox = 100;
%m = mass = 80 kg
% v = velocity,36m/s
% g = gravitational acceleration = 9.81 m/s^2
%x1 = lower guess (given 0.1)
%xu = upper guess (given 0.2)
% t = time = 4s
%root = root finding (cd, mass, time, etc)
%eap = approximate relative error
% es = desired relative error, <=2%
%fx = function value at root
figure
hold on
while (1)
xold = xr; % previous xr value for comparision for approximate relative error
xr = (xlower +xupper)/2; % solve mean for interval
iteration = iteration +1; % increment for iterations
if xr ~= 0 % if mean doesnt equal 0
eapprox = abs(xr -xold / xr)*100; % solving approximate relative error for xl and xu
end %end of first if statement
test = funct(xlower)*funct(xr); %testing to see if interval needs to be moved left or right
if test > 0 % positive
xlower = xr;
else
if test < 0 %negative
xupper = xr;
else
eapprox = 0; % if there is 0 approximate error that means the root is found
end %end of second if statement
if eapprox <= deserr
break,end %end of if statements
end %end of while loop
%disp(xr)
plot(iteration,xr,'ro')
%hold on
plot(iteration,eapprox, 'bo')
root = xr;
drawnow;
end %end of fucntion
end
Armin Motallebi
Armin Motallebi le 21 Juin 2022

0 votes

clear
clc
%User Defined Properties
serialPort = 'COM7'; % define COM port #
plotTitle = 'Serial Data Log'; % plot title
xLabel = 'Elapsed Time (s)'; % x-axis label
yLabel = 'Acceleration'; % y-axis label
plotGrid = 'on'; % 'off' to turn off grid
min = -1.5; % set y-min
max = 2.5; % set y-max
scrollWidth = 10; % display period in plot, plot entire data log if <= 0
delay = .0000001; % make sure sample faster than resolution
%Define Function Variables
time = 0;
data = zeros(3,1);
count = 0;
%Set up Plot
plotGraph = plot(time,data(1,:),'-r',...
'LineWidth',2,...
'MarkerFaceColor','w',...
'MarkerSize',2);
hold on
plotGraph1 = plot(time,data(2,:),'-m',...
'LineWidth',1,...
'MarkerFaceColor','w',...
'MarkerSize',2);
hold on
plotGraph2 = plot(time,data(3,:),'-b',...
'LineWidth',1,...
'MarkerFaceColor','w',...
'MarkerSize',2);
title(plotTitle,'FontSize',25);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
axis([0 10 min max]);
grid(plotGrid);
%Open Serial COM Port
s = serial(serialPort, 'BaudRate', 115200)
disp('Close Plot to End Session');
fopen(s);
tic
while ishandle(plotGraph) && ishandle(plotGraph2) && ishandle(plotGraph1) %Loop when Plot is Active
dat = fscanf(s,'%f'); %Read Data from Serial as Float
if(~isempty(dat) && isfloat(dat)) %Make sure Data Type is Correct
count = count + 1;
time(count) = toc; %Extract Elapsed Time in seconds
data(:,count) = dat(:,1); %Extract 1st Data Element
%Set Axis according to Scroll Width
if(scrollWidth > 0)
set(plotGraph,'XData',time(time > time(count)-scrollWidth),...
'YData', data(3,time > time(count)-scrollWidth));
set(plotGraph1,'XData',time(time > time(count)-scrollWidth),...
'YData', data(2,time > time(count)-scrollWidth));
set(plotGraph2,'XData',time(time > time(count)-scrollWidth),...
'YData', data(1,time > time(count)-scrollWidth));
axis([time(count)-scrollWidth time(count) min max]);
else
set(plotGraph,'XData',time,'YData',data(3,:));
set(plotGraph1,'XData',time,'YData',data(2,:));
set(plotGraph2,'XData',time,'YData',data(1,:));
axis([0 time(count) min max]);
end
%Allow MATLAB to Update Plot
pause(delay);
end
end
%Close Serial COM Port and Delete useless Variables
fclose(s);
clear count dat delay max min plotGraph plotGraph1 plotGraph2 plotGrid...
plotTitle s scrollWidth serialPort xLabel yLabel;
disp('Session Terminated');
prompt = 'Export Data? [Y/N]: ';
str = input(prompt,'s');
if str == 'Y' || strcmp(str, ' Y') || str == 'y' || strcmp(str, ' y')
%export data
csvwrite('accelData.txt',data);
type accelData.txt;
else
end
clear str prompt;

Catégories

En savoir plus sur Creating, Deleting, and Querying Graphics Objects dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by