matlab - two variables least squares function approximation - matlab

I have a function of two variables of the type: y = f(x1,x2) to be approximated and I would like to use least squares method to do it.
Polyval and Polyfit work with two-dimensional function, here I need to solve a three-dimensional function.
Thanks in advance.
G.B.

I've solved it in this way
A = [x1.^2,x1.*x2,x2.^2,x1,x2,ones(length(x1),1)];
c=A\y;
yEval = c(1)*x1.^2+c(2)*x1.*x2+c(3)*x2.^2+c(4)*x1+c(5)*x2+c(6);
Thanks anyway for your help.
Regards,
G.B.

Looking at your function, it looks like you're using a full quadratic response surface to fit against.
You could use x2fx function to generate all the terms. It's nothing groundbreaking here, but it might be a little cleaner. You could also use it to do not just OLS fitting, but also use the robust methods.
Here's some code I've written:
% set up terms for the variables, linear, quadratic, interactive, and constant
paramVEcomponents= x2fx([MAPkpa,RPM],'quadratic');
% robust fit using a Talwar weighting function
[coefs,robuststats]= robustfit(paramVEcomponents(2:6),(CAM2.*TEMPd./MAPkpa),'talwar');
% generating points for all the data we have based on the new parameters of the response surface
GMVEhat= paramVEcomponents * coefs;

Related

Double numerical integration in Matlab - Singularity

I have discrete data of a 2D function defined as
theta = linspace(0,pi,nTheta);
phi = linspace(0,2*pi,nPhi);
p=zeros(nPhi,nTheta);%only to show the dimension of my matrix
[np,nt]=ndgrid(phi,theta);
f1 = griddedInterpolant(np,nt,p,'spline');
f2= #(np,nt) f1(np,nt);
integral2(f2,0,2*pi,0,pi)
Note that p is calculated from a complex physical problem, but i showed above how it is initialized.
Also, I can increase nTheta and nPhi, which leads to more accurate calculation of p.
My calculated function (with nPhi=400,nTheta=200) is something like:
I tried 3 ways :
using Trapz function
using the code above but with linear interpolation for gridded interpolant
using the code above with spline interpolation
Although the spline is better than others, i still need to increase nPhi and nTheta, which makes it impossible for me to do the simulation due to its cost.
Is there any suggestion except these 3 methods or any general suggestion how i can do this computation more efficient? (I also took advantage of the symmetry in both directions)
Note that the shape of my function varies in each time step, so a local mesh refinement might be challenging because i don't know the detail of my function in advance.

MATLAB regress function and Normalizing Data

I am trying to perform a multiple linear regression in MATLAB using the regress function, and I am using a number of different variables that involve different scales and units. I am assume the answer to this question is yes, but should I normalize each variable before running the regression? I'm not sure if MATLAB does so automatically. Thanks for the help!
Yes, you should. If you want to normalize it between 0 and 1, you could use mat2gray function (assuming "vector" as your list of variables).
norm_vect = mat2gray(vector);
This function is used to convert matrix into an image, but works well if you don't want to write yours. You also can use a simple normalization like:
for i = 1:length(vector)
norm_vect(i) = (vector(i)-min(vector))/(max(vector)-min(vector));
end

how to get the area of a region using matlab

I want to plot some equations and inequalities like x>=50, y>=0,4x-5y>=8,x=40,x=60,y=25, y=45 in matlab and want to get the area produced by intersecting these equations and inequalities. Is it possible using matlab? If yes can someone provide me some manual? If not, is there some other software that can do this?
Integrals would work for your purposes, provided you know the points at which the curves intersect (something Matlab is also able to compute). Take a look at the documentation on the integral function.
q = integral(fun,xmin,xmax) approximates the integral of function fun
from xmin to xmax using global adaptive quadrature and default error
tolerances.
EDIT: As an additional resource, take a look at the code provided by user Grzegorz Konz on the Mathworks blog.
EDIT #2: I'm not familiar with any Matlab functions that'll take a vector of functions and return the points of intersection (if any) between all the curves. Users have produced functions that return the set of intersection points between two curves. You could run this function for each pair of equations in your list and use a function like polyarea to compute the area of the enclosed region if the curves are all straight lines.

newton raphson method in matlab

I would like to solve one equation in Matlab with two unknown variables using the Newton raphson method.
The equation is
I(:,:,K) = IL(:,:,K)-Io(:,:,K)*(exp((V+I*Rs)/a(:,:,K))-1)-((V+I*Rs(:,:,K))/Rsh(:,:,K));
Can this be done in matlab and if so please guide me since I have not managed to find anything related to this equation form!
Thanks
No. In general, one equation in two unknowns has an infinite number of solutions. (Think of a contour plot. You are essentially looking for the level set, the locus of all points that yields zero for the dependent variable. It will be a curvilinear path in those variables.)
So you can't "solve" it. I would suggest a good solution to visualize the locus is indeed the function contour. ezplot will do it even better.

MATLAB: Plot integral using quad/quadl

I would like to know if anybody knows how I can plot an integral calculated using quad/quadl, or if this is possible.
I read that I can set the trace parameter to be non-zero, and this results in the information of each iteration being provided, but I'm not sure how and if I can use the information to plot an integral.
Thanks.
quad and quadl do not compute an integral function anyway, i.e., an integral as a function of the parameter. And since tools like this work iteratively, refining their estimate until it satisfies a tolerance on the global value, they are not easily made to produce the plot you desire.
You can do what you desire by using a differential equation solver to generate the solution, ode45 for example.