Está en la página 1de 5

Write a user-defined MATLAB function for the following math function:

y(x)=(-0.2x^3 + 7x^2)e^{-0.3x}
The input to the function is x and the output is y. Write the function such that x can be a
vector (use element-by-element operations).
(a) Use the function to calculate y when x=[-1.5 5]
(b) Use the function to make a plot of the function for x= -1:1:6.

clear, clc
gamma=0.696; r=0.35; d=0.12; t=0.002;
coat=@(r,d,t,gamma) gamma*t*pi^2*(2*r+d)*d;
weight=coat(r,d,t,gamma);
fprintf('The required weight of gold is %.5f lb\n',weight)
Command Window:
The required weight of gold is 0.00135 lb

function [ M ] = monthlydeposit( S,r,N )


%UNTITLED10 Summary of this function goes here
% Detailed explanation goes here

M=S.*((r/1200)/((1+(r/1200)).^(12.*N)-1))
end

monthlydeposit( 25000,4.25,10 )

134 F 195 F

function [ HI ] = HeatIn( T,R )


%UNTITLED5 Summary of this function goes here
% Detailed explanation goes here

C=[-42.379 2.04901523 10.14333127 -0.22475541 -6.83783e-3 -5.481717e-2 1.22874e-3


8.5282e-4 -1.99e-6]
TR=[1, T, R, (T.*R), (T.^2), (R.^2), (T.^2.*R), (T.*R.^2), (R.^2).*( T.^2)]'

HI=C*TR

end

function [y] = fact(x)


if x<0
y=0;
fprintf('Error: Negative number inputs are not allowed\n\n')
elseif floor(x)~=x
y=0;
fprintf('Error: Non-integer number inputs are not allowed\n\n')
elseif x==0
y=1;
else
y=1;
for k=1:x
y=y*k;
end
end

clear, clc
disp('Part (a)')
x=12;
y = fact(x);
if y>0
fprintf('The factorial of %i is %i\n\n',x,y)
end
disp('Part (b)')
x=0;
y = fact(x);
if y>0
fprintf('The factorial of %i is %i\n\n',x,y)
end
disp('Part (c)')
x=-7;
y = fact(x);
if y>0
fprintf('The factorial of %i is %i\n\n',x,y)
end
disp('Part (d)')
x=6.7;
y = fact(x);
if y>0
fprintf('The factorial of %i is %i\n\n',x,y)
end

function w = crosspro(u,v)
n=length(u);
if n == 2
u(3)=0;
v(3)=0;
end
w(1)=u(2)*v(3)-u(3)*v(2);
w(2)=u(3)*v(1)-u(1)*v(3);
w(3)=u(1)*v(2)-u(2)*v(1);

u=[3 11];v=[14 -7.3]


x=[-6 14.2 3]; y=[6.3 -8 -5.6]
crosspro( u,v )
crosspro(x,y)

function [theta, radius]=CartesianToPolar(x,y)


radius= sqrt(x^2+y^2);
theta=acos(abs(x)/radius)*180/pi;
if (x<0)&(y>0)
theta=180-theta;
end
if (x>0)&(y<0)
theta=-theta;
end
if (x<=0)&(y<0)
theta=theta-180;
end
Command Window:
>> [th_a, radius_a]=CartesianToPolar(14,9)

th_a = 32.7352; radius_a = 16.6433

>> [th_a, radius_a]=CartesianToPolar(-11,-20)

th_a = -118.8108; radius_a = 22.8254

>> [th_a, radius_a]=CartesianToPolar(-15,4)

th_a = 165.0686; radius_a = 15.5242

>> [th_a, radius_a]=CartesianToPolar(13.5,-23.5)

th_a = -60.1240; radius_a = 27.1017

También podría gustarte