MATLAB HELP ME PLS

조회 수: 1 (최근 30일)
Triggs
Triggs 2019년 7월 18일
댓글: Walter Roberson 2019년 8월 19일
close all
clear
clc
format long
a=0;
b=10;
function [Distance] = trapezoidal(Speed, a, b, n)
h = (b-a)/n;
result = 0.5*Speed*a + 0.5*Speed*b;
for i = 1:(n-1)
result = result + Speed*(a + i*h);
end
[Distance] = h*result;
fprintf('ans=%6.6ff\n',Distance)
end
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 8월 19일
It is not clear what the question is?

댓글을 달려면 로그인하십시오.

답변 (1개)

TADA
TADA 2019년 7월 18일
편집: TADA 2019년 7월 18일
The trapezoidal integration method is a numeric calculation which approximates the area trapped between the curve and the x axis by dividing the curve to segments. the area of each segment is calculated by calculating the area of a trapeze entrapped by the coordinates of that segment.
function [auc, integration] = trapezoidal(x,y)
dx = diff(x);
integration = ((y(1:end-1)+y(2:end)) .* dx) / 2;
auc = sum(integration);
end
Or simply use trapz
The accuracy of this method is limited mainly by the size of the segments. smaller dx means more accurate results
so define your time vector with more elements:
t = linspace(0,10,1000);
  댓글 수: 1
TADA
TADA 2019년 7월 18일
to iteratively improve the approximation as required, I would start from a small number of segments, lets say 10 segments like you did
then check the error of the calculated distance
To calculate the actual distance you can integrate the polynomial:
% velocity polynomial coefficients
vp = [0.0011, -0.02928, 0.2807, -1.1837, -0.8283, 41.234, -3.3549];
tRange = [0 10];
nSegments = 10;
% call this in a loop that improves the precesion
while logical condition
% do something to improve precesion here
% calculate distance and error again
[d, err] = tryTrapz(tRange, vp, nSegments);
fprintf('Your message');
end
function [d, err] = tryTrapz(tRange, vp, nSegments)
t = linspace(tRange(1), tRange(2), nSegments);
d = trapz(t, polyval(vp, t));
% distance polynomial coefficients = integrated velocity
dp = polyint(vp);
theoreticalDist = polyval(dp, t(end));
err = 100*(theoreticalDist - d)/theoreticalDist;
end

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Polynomials에 대해 자세히 알아보기

태그

아직 태그를 입력하지 않았습니다.

제품

Community Treasure Hunt

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

Start Hunting!

Translated by