Contents
ejercicio 1
function b=bernstein(n,i,t) b=combina(n,i).*t.^i.*(1-t).^(n-i); end function c=combina(n,i) c=factr(n)/(factr(i)*factr(n-i)); end function f = factr(n)
if n==0 f = 1; else f = n*factr(n-1); end %graf berntein t=linspace(0,1); n=3; for i=0:n b=bernstein(n,i,t); plot(t,b); hold on; end xlabel('t'); ylabel('Polinomio de Bernstein'); title('Polinomio Bernstein grado 3'); legend('B3,0','B3,1','B3,2','B3,3'); %graf berzier t=linspace(0,1); V=[1 2 4 4.6;1 3 -1 1.5]; plot(V(1,:),V(2,:)); n=size(V);n=n(2); s=size(t);s=s(2); x=zeros(n,s);y=zeros(n,s); for i=1:n x(i,:)=bernstein(n-1,i-1,t)*V(1,i); y(i,:)=bernstein(n-1,i-1,t)*V(2,i); end a=sum(x);b=sum(y); hold on; plot(a,b);
ejercicio 2
function f = factr(n)% función recursiva de nombre factr para calcular el factorial de n que se le da como parámetro. if n==0 f = 1; else f = n*factr(n-1); end velocidad=xlsread('sotaventogaliciaanual','Hoja1','B2:B52129'); interpolar si es necesario if any(isnan(velocidad)) %si hay algún NaN x=1:length(velocidad); i=find(~isnan(velocidad)); velocidad=interp1(x(i),velocidad(i),x); end %histograma x=0.5:1:max(velocidad); horas=hist(velocidad,x); %convierte a frecuencias y ajusta a la función de Weibull frec=horas/sum(horas); f=@(a,x) (a(1)/a(2))*((x/a(2)).^(a(1)-1)).*exp(-(x/a(2)).^a(1)); a0=[2 8]; %valor inicial de los parámetros af=nlinfit(x,frec,f,a0) hold on %diagrama de frecuencias bar(x,frec,'c'); %representa la curva de ajuste x=linspace(0,max(velocidad),100); y=f(af,x); plot(x,y,'r') title('Ajuste a la función Weibull') xlabel('Velocidad') ylabel('Frecuencia') hold off %directamente de los datos de las velocidades del viento potencia=0.5*1.225*sum(velocidad.^3)/length(velocidad); fprintf('Potencia, directamente de las medidas: %3.1f\n',potencia); media=mean(velocidad); %función de distribución de Weibull % Parametros de ajsute de Weibull: k=2.0486;c=9.4165; potencia=0.5*1.225*media^3*gamma(1+3/k)/(gamma(1+1/k)^3); fprintf('Potencia, función Weibull: %3.1f\n',potencia); %función de distribución de Rayleigh potencia=0.5*1.225*6*media^3/pi; fprintf('Potencia, función Rayleigh: %3.1f\n',potencia); Pr=1500; x0=3.0;xr=14;x1=25; %datos de la curva de potencia potencia=xlsread('sotavento_curva potencia','Hoja1','B2:B27') x=0:0.5:25; pot=potencia(x>=3 & x<=12); hold on x=x0:0.5:xr-2; plot(x,pot,'ro','markersize',3,'markerfacecolor','r') title('Ajuste de la curva de potencia de un aerogenerador'); axis([0 15 -10 1550]) xlabel('velocidad') ylabel('potencia') grid on %ajuste p=polyfit(x,pot',3); %ajuste a un polinomio de tercer grado yp=polyval(p,x); plot(x,yp,'k') hold off %cálculo de la potencia media f=@(x) (k/c)*((x/c).^(k-1)).*exp(-(x/c).^k); %función de Weibull h=@(x) f(x).*polyval(p,x); power=quad(h,x0,xr)+Pr*quad(f,xr,x1); fprintf('La potencia media es: %3.1f\n',power)
ejercicio 3
t0=0;tf=40; periodo=[t0 tf]; cond_inicio=[1 0]; [t,x] = ode45('funcion_sistema',periodo,cond_inicio); plot(t,x(:,1),'-o') hold on [t,x] = ode45('funcion_sistema2',periodo,cond_inicio); plot(t,x(:,1),'-+') [t,x] = ode45('funcion_sistema3',periodo,cond_inicio); plot(t,x(:,1),'-*') title('Sistema Masa-Resorte-Amortiguador con ODE45'); xlabel('Tiempo, t (s)'); ylabel('Desplazamiento, x (m)'); legend('c=5','c=40','c=200') %funcion 1 function dxdt = odefun(t,x) m=20;k=20;c=5; dxdt = [x(2);-(1/m)*(c*x(2)+k*x(1))]; %funcion 2 function dx2dt = funcion_sistema(t,x) m=20;k=20;c=40; dx2dt = [x(2);-(1/m)*(c*x(2)+k*x(1))]; %funcion 3 function dx2dt = funcion_sistema(t,x)
m=20;k=20;c=200; dx2dt = [x(2);-(1/m)*(c*x(2)+k*x(1))];
ejercicio 4
function varargout = untitled(varargin) %UNTITLED MATLAB code file for untitled.fig % UNTITLED, by itself, creates a new UNTITLED or raises the existing % singleton*. % % H = UNTITLED returns the handle to a new UNTITLED or the handle to % the existing singleton*. % % UNTITLED('Property','Value',...) creates a new UNTITLED using the % given property value pairs. Unrecognized properties are passed via % varargin to untitled_OpeningFcn. This calling syntax produces a % warning when there is an existing singleton*. % % UNTITLED('CALLBACK') and UNTITLED('CALLBACK',hObject,...) call the % local function named CALLBACK in UNTITLED.M with the given input % arguments. % % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one % instance to run (singleton)". % % See also: GUIDE, GUIDATA, GUIHANDLES % Edit the above text to modify the response to help untitled % Last Modified by GUIDE v2.5 09-Jan-2019 18:44:30 % Begin initialization code - DO NOT EDIT gui_Singleton = 1; gui_State = struct('gui_Name', mfilename, ... 'gui_Singleton', gui_Singleton, ... 'gui_OpeningFcn', @untitled_OpeningFcn, ... 'gui_OutputFcn', @untitled_OutputFcn, ... 'gui_LayoutFcn', [], ... 'gui_Callback', []); if nargin && ischar(varargin{1}) gui_State.gui_Callback = str2func(varargin{1}); end if nargout [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); else gui_mainfcn(gui_State, varargin{:}); end % End initialization code - DO NOT EDIT % --- Executes just before untitled is made visible. function untitled_OpeningFcn(hObject, eventdata, handles, varargin) % This function has no output args, see OutputFcn. % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % varargin unrecognized PropertyName/PropertyValue pairs from the % command line (see VARARGIN) % Choose default command line output for untitled handles.output = hObject; % Update handles structure guidata(hObject, handles); % UIWAIT makes untitled wait for user response (see UIRESUME) % uiwait(handles.figure1); % --- Outputs from this function are returned to the command line. function varargout = untitled_OutputFcn(hObject, eventdata, handles) % varargout cell array for returning output args (see VARARGOUT); % hObject handle to figure % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Get default command line output from handles structure varargout{1} = handles.output; % --- Executes on button press in pushbutton1. function pushbutton1_Callback(hObject, eventdata, handles) % hObject handle to pushbutton1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) a=str2double(get(handles.edit1, 'string')); t0=str2double(get(handles.edit2, 'string')); p=str2double(get(handles.edit3, 'string')); hmin=str2double(get(handles.edit4, 'string')); fl=flecha(a,t0,p,hmin); long=longitud(a,t0,p,hmin); set(handles.edit5, 'string', fl) set(handles.edit6, 'string', long) function f=flecha(a,t0,p,hmin) f=t0/p*(cosh(p*a/(2*t0))-1); function l=longitud(a,t0,p,hmin) l=(t0/p)*(sinh(p*a/(2*t0))); % --- Executes on button press in pushbutton2. function pushbutton2_Callback(hObject, eventdata, handles) % hObject handle to pushbutton2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) a=str2double(get(handles.edit1, 'string')); t0=str2double(get(handles.edit2, 'string')); p=str2double(get(handles.edit3, 'string')); hmin=str2double(get(handles.edit4, 'string')); axes(handles.axes1); cat=catenaria(a,t0,p,hmin); x=linspace(-a/2,a/2); plot(x,cat) title('Calculadora de Catenarias') xlabel('Distancia (m)') ylabel('Altura (m)') axis([-a/2 a/2 0 hmin]) function c=catenaria(a,t0,p,hmin) x=linspace(0,a); c=t0/p*(cosh(p*(2.*x-a)/(2*t0))-cosh(p*a/(2*t0)))+hmin; function edit1_Callback(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of edit1 as text % str2double(get(hObject,'String')) returns contents of edit1 as a double % obtiene valor del slider edit slidev = get(handles.edit1,'String'); % convierte string a numero slidevn = str2num(slidev); % comprueba si no es un numero o si esta fuera de rango 0-100 if (isempty(slidev) || slidevn <0 || slidevn >1050) set(handles.slider3,'Value',0); set(handles.edit1,'String','0'); else set(handles.slider3,'Value',slidevn); end % --- Executes during object creation, after setting all properties. %function edit1_CreateFcn(hObject, eventdata, handles) % hObject handle to edit1 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end %function edit2_Callback(hObject, eventdata, handles) % hObject handle to edit2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of edit2 as text % str2double(get(hObject,'String')) returns contents of edit2 as a double % --- Executes during object creation, after setting all properties. function edit2_CreateFcn(hObject, eventdata, handles) % hObject handle to edit2 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end %function edit3_Callback(hObject, eventdata, handles) % hObject handle to edit3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of edit3 as text % str2double(get(hObject,'String')) returns contents of edit3 as a double % --- Executes during object creation, after setting all properties. %function edit3_CreateFcn(hObject, eventdata, handles) % hObject handle to edit3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end %function edit4_Callback(hObject, eventdata, handles) % hObject handle to edit4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of edit4 as text % str2double(get(hObject,'String')) returns contents of edit4 as a double % --- Executes during object creation, after setting all properties. %function edit4_CreateFcn(hObject, eventdata, handles) % hObject handle to edit4 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end % --- Executes on slider movement. %function slider3_Callback(hObject, eventdata, handles) % hObject handle to slider3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'Value') returns position of slider % get(hObject,'Min') and get(hObject,'Max') to determine range of slider % obtiene valor del slider slidev = get(handles.slider3,'Value'); % asigna valor del slider en edit text set(handles.edit1,'String',num2str(slidev)); % actualiza la estructura handle guidata(hObject,handles); % --- Executes during object creation, after setting all properties. %function slider3_CreateFcn(hObject, eventdata, handles) % hObject handle to slider3 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: slider controls usually have a light gray background. if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor',[.9 .9 .9]); end %function edit5_Callback(hObject, eventdata, handles) % hObject handle to edit5 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of edit5 as text % str2double(get(hObject,'String')) returns contents of edit5 as a double % --- Executes during object creation, after setting all properties. %function edit5_CreateFcn(hObject, eventdata, handles) % hObject handle to edit5 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white'); end %function edit6_Callback(hObject, eventdata, handles) % hObject handle to edit6 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles structure with handles and user data (see GUIDATA) % Hints: get(hObject,'String') returns contents of edit6 as text % str2double(get(hObject,'String')) returns contents of edit6 as a double % --- Executes during object creation, after setting all properties. %function edit6_CreateFcn(hObject, eventdata, handles) % hObject handle to edit6 (see GCBO) % eventdata reserved - to be defined in a future version of MATLAB % handles empty - handles not created until after all CreateFcns called % Hint: edit controls usually have a white background on Windows. % See ISPC and COMPUTER. if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor')) set(hObject,'BackgroundColor','white');
Error using dbstatus Error: File: C:\Users\docencia\Desktop\Nueva carpeta\matlab.m Line: 449 Column: 1 The function "edit2_CreateFcn" was closed with an 'end', but at least one other function definition was not. To avoid confusion when using nested functions, it is illegal to use both conventions in the same file.