# How to write function for two variables

69 visualizzazioni (ultimi 30 giorni)
Faizan Lali il 22 Feb 2023
Commentato: Faizan Lali il 23 Feb 2023
I am trying to write down the function to predict "y" having two independent variables "x1 & x2". Here is my code for the function;
function y=Project_func(beta0,x1,x2)
y=100./(1+exp((-beta0(1).*(-2.4-(39.7*(1+x1).^(-2.8))))+(beta0(2).*(-2.4-(39.7*(1+x1).^(-2.8))).*log10(100.*x2))));
end;
but once I run the code it does not spit the y values as desired because I have predicted values taken from the excel. I want to solve it in the Matlab. Here are the equations. C1 & C2 are parameters having intial guess.
##### 7 CommentiMostra 5 commenti meno recentiNascondi 5 commenti meno recenti
dpb il 22 Feb 2023
Modificato: dpb il 22 Feb 2023
%figure;
%hold on
%set(gca, 'fontsize',14,'fontweight','bold');
for i=1:p
h2(i) = mesh(X,Y,Xp(:,:,i));
end
Unrecognized function or variable 'p'.
%plot C vs t to know the total span
ypred=fnameFOR(beta0,X,Y);
h2(i+1)=mesh(X,Y,ypred);
You've not provided any data nor even described the variables; so nobody can see your workspace from here to know...but the above code tries to put p mesh plots on the same axis which will look very messy. Probably about all it will leave visible will be the result for whichever plane of the array is the largest in amplitude, it occluding the others.
Then, you add one more yet on top of those; one presumes fnameFOR is the name for the preceding function m-file; that's a very non-informative name, but MATLAB doesn't really care.
Now, the h array will contain the handles to the various mesh plot objects created; what, specifically you had in mind to do to them after plotting is unclear as to why you did save the handles; they're only of use to be able to make changes to the properties of the plot afterwards if desired. Or, you could hide all of them except one by setting 'Visible','off' so could see what each plane did look like.
It's certainly unclear what you think isn't working...
Faizan Lali il 23 Feb 2023
Yeah, i am sorry for not providing the data vectors, as data sheet is kind a mess and you wont understand untill explain. i can attach excel sheet and my code for you if you want, but let me explain to make it clear.
Xp vector is 50x50x2, means it contains two parameter of the functions. Now I want to plot each parameter Xp1 and Xp2 against two variables X and Y. Also I want to plot function value as Z above them. So total of 3 surfaces on a meshgrid against X and Y. I did it by another way i can share the part of code and the figure for your reference.
So normally in 2D handle option works well while plotting over each other and then playing with the properties of the figure as you mentioned, but I am not sure how it will work for meshgrid.
Moreover, once I go to change the color scheme for each surface generated it dosen,t let me do it.
Now I figure it out this way
ypred=fnameFOR(beta0,X,Y);
Xp1=Xp(:,:,1);
Xp2=Xp(:,:,2);
figure;
mesh(X,Y,Xp1);
hold on
mesh(X,Y,Xp2);
mesh(X,Y,ypred);
legend('\beta_1*\partialY/\partial(\beta_1)','\beta_2*\partialY/\partial(\beta_2)','Y');
xlabel('X'); ylabel('Y'); zlabel('Parameters and Z');

Accedi per commentare.

### Risposta accettata

dpb il 22 Feb 2023
I'd approach writing the functional something more like--
function y=Y(C,x1,x2)
% Magic function Y=fn(C, x1, x2) from unknown source
% C is 2-vector of C1, C2 per Eqn 1
C1=C(1); C2=C(2); % convenience in writing expression w/o subscripts
C2S=@(x) -2.40874-39.748*(1+x).^-2.856; % define C2* as anonymous function(x)
y=100./(1+exp(-C1.*C2S(x1)+C2.*C2S(x1).*log10(100*x2))); % the function (x1,x2) for input C
end;
If we had the experimental or other data, be interesting to see how well it would fit...but, we're pretty much hamstrung at this point.
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Categorie

Scopri di più su 2-D and 3-D Plots in Help Center e File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!

Translated by