why is my code not working?

1 view (last 30 days)
Anjoud Alkathairi
Anjoud Alkathairi on 14 Nov 2022
Commented: Anjoud Alkathairi on 14 Nov 2022
v=[2;0;1;9;0;1;3;0;1];
m=max(v);
n=mean(v);
f=@(x) m-(1/2)*x;
g=@(x) 2*x^2-n;
figure
ezplot(f);
hold on
ezplot(g);
hold on
syms x y
eqns=y==m-(1/2)*x;y=2*x^2-n;
[solx, soly]=solve(eqns,[x y]);
plot(solx(1),soly(1),'b-x',solx(2),soly(2),'b-x')
expr1=m-(1/2)*x;
expr2=2*x^2-n;
I1=int(expr1,[solx(1) solx(2)]);
I2=int(expr2,[solx(1) solx(2)]);
I=I2-I1;
fprintf('The area bounded by the two curves is: %f\n',I);

Answers (1)

Stephan
Stephan on 14 Nov 2022
v=[2;0;1;9;0;1;3;0;1];
m=max(v);
n=mean(v);
f=@(x) m-(1/2).*x; % Vectorized
g=@(x) 2.*x.^2-n; % Vectorized
figure
ezplot(f);
hold on
ezplot(g);
hold on
syms x y
eqns=[y==m-(1/2)*x;y==2*x^2-n]; % Array and y==2 instead of y=2 in equation 2
[solx, soly]=solve(eqns,[x y]);
plot(solx(1),soly(1),'b-x',solx(2),soly(2),'b-x')
expr1=m-(1/2)*x;
expr2=2*x^2-n;
I1=int(expr1,[solx(1) solx(2)]);
I2=int(expr2,[solx(1) solx(2)]);
I=I2-I1;
fprintf('The area bounded by the two curves is: %f\n',I);
The area bounded by the two curves is: 34.022481

Community Treasure Hunt

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

Start Hunting!