Plotting data within while loop,help

18 次查看(过去 30 天)
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 个评论
Alexa Shumaker
Alexa Shumaker 2019-1-29
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 2019-1-29
编辑:Bob Thompson 2019-1-30
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.

请先登录,再进行评论。

回答(2 个)

Harshit Jain
Harshit Jain 2019-2-7
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 2022-6-21
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;

类别

Help CenterFile Exchange 中查找有关 Geometry and Mesh 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by