Anteprima
Vedrai una selezione di 4 pagine su 13
Esame Informatica Pag. 1 Esame Informatica Pag. 2
Anteprima di 4 pagg. su 13.
Scarica il documento per vederlo tutto.
Esame Informatica Pag. 6
Anteprima di 4 pagg. su 13.
Scarica il documento per vederlo tutto.
Esame Informatica Pag. 11
1 su 13
D/illustrazione/soddisfatti o rimborsati
Disdici quando
vuoi
Acquista con carta
o PayPal
Scarica i documenti
tutte le volte che vuoi
Estratto del documento

Codice C++ per calcolare derivate di una funzione

//fk[i]=funz(xk);// formula derivata prima avantifd1k_a[i]=(funz(xk+h)-funz(xk))/h;// formula derivata seconda avantifd2k_a[i]=(funz(xk+2*h)-2*funz(xk+h)+funz(xk) )/(h*h);// formula derivata terza avantifd3k_a[i]=(-3*funz(xk+2*h)+3*funz(xk+h)-funz(xk)+funz(xk+3*h) )/(h*h*h);}for(int i=0; i<n; i++){double xk = a + i*h;//fk[i]=funz(xk);fout<< setw(5) << setprecision(4) << fixed << xk << " " //ascissa xk<< setw(10) << setprecision(7) << fixed << fd3k_a[i] << " "<< setw(10) << setprecision(7) << fixed << fd2k_a[i] << " "<< setw(10) << setprecision(7) << fixed << fabs(fd2k_a[i]-deriv2(xk)) << "\n";}fout.close();return 0;}double funz(double x){double y=exp(-2*x)*cos(3*x);return y;} file:///home/andreavezzadini/preparazione_esame/esameDIFF2.cpp7/27/21 esameDIFF2.cpp 2double deriv1(double x){double y=

-exp(-2*x)*(3*sin(3*x)+2*cos(3*x));return y;
double deriv2(double x){double y= exp(-2*x)*(12*sin(3*x)-5*cos(3*x));return y;}
double deriv3(double x){double y= exp(-2*x)*(46*cos(3*x)-9*sin(3*x));return y;}
file:///home/andreavezzadini/preparazione_esame/esameDIFF2.cpp7/27/21 esameDIFFi.cpp 1#include <iostream>#include <fstream>#include <iomanip>#include <cmath>using namespace std;#define NINT (40)double funz(double x);double deriv1(double x);double deriv2(double x);double deriv3(double x);int main(){double fk[NINT+1];double fd1k_i[NINT+1];double fd2k_i[NINT+1];double fd3k_i[NINT+1];ofstream fout("graph_es2.dat");int n=NINT+1;double a=0.;double b=1.4;double h=(b-a)/(n-1);for(int i=3; i<n; i++){double xk = a + i*h;//fk[i]=funz(xk);// formula derivata prima indietrofd1k_i[i]=(funz(xk)-funz(xk-h))/h;// formula derivata seconda indietrofd2k_i[i]=(funz(xk-2*h)-2*funz(xk-h)+funz(xk) )/(h*h);// formula derivata terza
indietrofd3k_i[i]=(3*funz(xk-2*h)-3*funz(xk-h)+funz(xk)-funz(xk-3*h) )/(h*h*h);
for(int i=3; i<n; i++){
    double xk = a + i*h;
    //fk[i]=funz(xk);
    fout << setw(5) << setprecision(4) << fixed << xk << " " //ascissa xk
    //<< setw(10) << setprecision(7) << fixed << fk[i] << " "<< setw(10) << setprecision(7) << fixed << fd1k_i[i] << " "<< setw(10) << setprecision(7) << fixed << fd2k_i[i] << " "<< setw(10) << setprecision(7) << fixed << fabs(fd2k_i[i]-deriv2(xk)) << " "<< setw(10) << setprecision(7) << fixed << fd3k_i[i] << " "<< setw(10) << setprecision(7) << fixed << fabs(fd3k_i[i]-deriv3(xk)) << "\n ";
}
fout.close();
return 0;
}

double funz(double x){
    double y=exp(-2*x)*cos(3*x);
    return y;
}
Formattazione del testo
file:///home/andreavezzadini/preparazione_esame/esameDIFFi.cpp7/27/21 esameDIFFi.cpp
2double deriv1(double x){
    double y= -exp(-2*x)*(3*sin(3*x)+2*cos(3*x));
    return y;
}
double deriv2(double x){
    double y= exp(-2*x)*(12*sin(3*x)-5*cos(3*x));
    return y;
}
double deriv3(double x){
    double y= exp(-2*x)*(46*cos(3*x)-9*sin(3*x));
    return y;
}

file:///home/andreavezzadini/preparazione_esame/esameDIFFi.cpp7/27/21 esameFARSI.cpp
1#include<iostream>
#include<fstream>
#include<iomanip>
#include<cmath>
using namespace std;

const int NINT (8);

double funz(double);

int main () {
    ofstream fout("esercizio3_graph_regula.dat");
    double a = -1;
    double b = 1;
    double xk_1 = 0.375;
    double xk = 0.56;
    double z0 = 0.51;
    for(int k = 0; k < NINT; k++){
        double xk1 = xk - funz(xk) * (xk - xk_1) / (funz(xk) - funz(xk_1)); //funzione regula falsi
        fout << setw(5) << setprecision(7) << fixed << k+1 << " " //iterazioni
        << setw(15) << setprecision(7) <<
Il testo formattato con i tag HTML è il seguente:

fixed << xk1 << " " //stima dello zero approssimato xk1<< setw(15) << setprecision(7) << fixed << fabs(xk1 - z0) / fabs(xk - z0) << " "<< setw(15) << setprecision(7) << fixed << fabs(xk1 - z0) / pow(fabs(xk - z0),2) << " "<< setw(15) << setprecision(7) << fixed << funz(xk1) << '\n';
if(funz(xk1) * funz(xk_1) >= 0) xk_1 = xk ; // non possono essere invertiti !xk = xk1 ;
}
fout.close();
return 0;
}

double funz(double x){
return exp(6*x)*(x-(0.51))*(x+8)*(x+10);
}

file:///home/andreavezzadini/preparazione_esame/esameFARSI.cpp7/27/21 esameGAUSS.cpp 1

#include <iostream>
#include <fstream>
#include <math.h>
#include <iomanip>

using namespace std;

double funz(double x);

#define NG (4)

int main(){
double psik[NG]; // punti di Gauss
double wk[NG]; // pesi di Gauss
int n=NG; // numero punti gauss
double b=1., a=0.;
double jac=0.5*(b-a);

```html

jacobianodouble I0=97.-35*M_E;ofstream fout("esame5_gauss.dat");for(int ng=1;ng<=n;ng++){switch(ng){case 1:psik[0]=0.;wk[0]=2;break;case 2:psik[0]=-0.5773502696;psik[1]=0.5773502696;wk[0]= 1.;wk[1]= wk[0];break;case 3:psik[0]=-0.7745966692;psik[1]=0;psik[2]=0.7745966692;wk[0]=0.5555555555556;wk[1]=0.8888888888889;wk[2]=wk[0];break;default:cout << "Troppi punti " << endl;}double Ig=0.; // valore dell'Integralefor(int k=0;k

fixed << fabs(Ig-I0)/Ig <<" \n"; //errrore}
fout.close();
return 0;
}

double funz(double x){
    return cos(x);
}

file:///home/andreavezzadini/preparazione_esame/esameGAUSS.cpp7/27/21 esameMOTO.cpp

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>

using namespace std;

double f(double t);

#define N (100)

int main(){
    double xt[N+1];
    double t0=0.; //istante iniziale intervallodouble tf=5.; //istante finale itervallodouble x0=2.5; //posizione inizialedouble v0=1.4; //velocità inizialedouble dt=(tf-t0)/N;double M=2.; //la funzione generale è Mx'' + Wx' + Kx = F(t)double W=0.; //quindi bisogna assegnare il coefficiente dell'esame al proprio M,W,kdouble K=7.;ofstream fout("esameMOTO.dat");xt[0]=2.5;fout << setw(10) << setprecision(5) << fixed << 0. << " " << setw(10) << setprecision(5) << fixed <<xt[0] << " " <<
setw(10) << setprecision(5) << fixed << 0.5*pow(xt[0]/(dt), 2) << "\n";
xt[1]=x0+v0*dt;
fout << setw(10) << setprecision(5) << fixed << dt << " " << setw(10) << setprecision(5) << fixed <<xt[1] << " " << setw(10) << setprecision(5) << fixed << 0.5*pow((xt[1]-xt[0])/(dt), 2) << "\n";
for(int i=2; i<=N; i++){
    xt[i]= ((2*M+W*dt)*xt[i-1]-M*xt[i-2]+f(i*dt)*dt*dt)/(M+W*dt+K*dt*dt);
    fout << setw(10) << setprecision(5) << fixed << i*dt << " "<< setw(10) << setprecision(5) << fixed << xt[i] << " "<< setw(10) << setprecision(5) << fixed << 0.5*pow((xt[i]-xt[i-1])/(dt),2)<< "\n";
}
fout.close();
return 0;
}

double f(double t){
    return cos(t);
}
file:///home/andreavezzadini/preparazione_esame/esameMOTO.cpp7/27/21 esameNEWTON.cpp
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>

using namespace std;

double funz(double x);
double deriv(double x);

#define NINT (8)

int main(){
    int n=NINT;
    double xk=0.375;
    double z0=0.51;
    ofstream fout("graph5.dat");
    
    for(int k=0; k<n; k++){
        double fxk=funz(xk);
        double xk1=xk-fxk/deriv(xk); //zero di newton
        
        fout << setw(5) << setprecision(7) << fixed << k+1 << " " //iterazioni
        << setw(15) << setprecision(7) << fixed << xk1 << " " //stima dello zero approssimato xk1
        << setw(15) << setprecision(7) << fixed << fabs(xk1 - z0) / fabs(xk - z0) << " " 
        << setw(15) << setprecision(7) << fixed << fabs(xk1 - z0) / pow(fabs(xk - z0),2) << " "
        << setw(15) << setprecision(7) << fixed << funz(xk1) << '\n';
        
        /*if(fabs(xk1-xk)<TOL){
            cout <<
        */
    }
}

"convergente iter=" << i << " xk=" << xk1 << "errore " << fabs(xk1-xk) << "\n";return 0;}
*/xk=xk1;}//cout << "non convergente iter="<< n << " xk="<< xk << "\n";fout.close();return 0;}
double funz(double x){return exp(3*x)*(x-(0.51))*(x+8)*(x+10);}
double deriv(double x){return exp(3*x)*(3*pow(x,3)+ 55.47*pow(x,2) + 247.44*x-51.58);}
file:///home/andreavezzadini/preparazione_esame/esameNEWTON.cpp7/27/21 esameRETT.cpp 1
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cmath>
using namespace std;
double funz(double x);
#define NINT (4)
#define NRAFF (10)
int main(){
ofstream fout("esame5_rett.dat");
int n=NINT;
double b=1.;
double a=0.;
double I0=97-35*M_E;
double d_errnd2=1.; //inizializzazione dell'errore
double s_errnd2=1.; //inizializzazione dell'errore
for(int k=0; k<NRAFF; k++){
double h=(b-a)/(n);

//step double xk=a; //inizializzazione variabili double xk1=xk+h; double d_rect=0.; double s_rect=0.; for(int i=0; i<n; i++){ s_rect=s_rec
Dettagli
Publisher
A.A. 2021-2022
13 pagine
1 download
SSD Scienze matematiche e informatiche INF/01 Informatica

I contenuti di questa pagina costituiscono rielaborazioni personali del Publisher AndreVezz di informazioni apprese con la frequenza delle lezioni di Fondamenti di informatica e studio autonomo di eventuali libri di riferimento in preparazione dell'esame finale o della tesi. Non devono intendersi come materiale ufficiale dell'università Università degli Studi di Bologna o del prof Manservisi Sandro.