Solve non linear equation with vector
6 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Dear,
I would like to solve a non linear equation, on the form:
yv = 1:1:19;
for k1 = 1:length(yv)
y = yv(k1);
f = @(x) exp(-0.5*((x-45)/dados.mass(k1))^2) == dados.par(k1) ;
x(k1) = fzero(f, 0.5);
end
Send : x the variable
dados.mass(k1) vector with value, contain 19 lines
dados.par (k1) equal aspect to top.
I would like to solve each equation related a value of dados.mass(1) until (19) concomitantly at dados.par(1) until (19), obtaining the vector x with respectivly values.
Can help me, please?
Yours faithfully
0 commentaires
Réponses (3)
Torsten
le 11 Août 2023
Déplacé(e) : Torsten
le 11 Août 2023
x(k1) = 45+dados.mass(k1)*sqrt(-2*log(dados.par(k1)))
or
x(k1) = 45-dados.mass(k1)*sqrt(-2*log(dados.par(k1)))
1 commentaire
Sam Chak
le 11 Août 2023
@Torsten, 👍 That's a good analytical solution without using fzero()! Did I plot the visualization of the solutions correctly?

m = linspace(0, 300, 31);
p = linspace(0, 1, 31); % assuming that Gaussian function has a range of 0 to 1
[M, P] = meshgrid(m, p);
X1 = 45 - sqrt(2)*M.*sqrt(log(1./P));
X2 = 45 + sqrt(2)*M.*sqrt(log(1./P));
tiledlayout(2,2);
nexttile
surf(M, P, X1), xlabel('mass'), ylabel('par'),
title({'$x^{-}$'}, 'interpreter', 'latex', 'fontsize', 16)
nexttile
surf(M, P, X2), xlabel('mass'), ylabel('par'),
title({'$x^{+}$'}, 'interpreter', 'latex', 'fontsize', 16)
nexttile
contourf(M, P, X1), xlabel('mass'), ylabel('par')
nexttile
contourf(M, P, X2), xlabel('mass'), ylabel('par')
Star Strider
le 11 Août 2023
It looks like you want to do a nonlinear regression.
Perhaps this —
dados = array2table(sortrows([10*randn(12,1)+35,rand(12,1)],1), 'VariableNames',{'mass','par'})
f = @(x,m) exp(-0.5*((x-45)./m).^2) ;
x0 = rand;
mdl = fitnlm(dados, f, x0)
[y,yci] = predict(mdl, dados.mass);
figure
hp1 = plot(dados.mass, dados.par, 'b.', 'DisplayName','Data');
hold on
hp2 = plot(dados.mass, y, 'DisplayName','Regression');
hp3 = plot(dados.mass, yci, '--r', 'DisplayName','95% Confidence Intervals');
hold off
grid
xlabel('mass')
ylabel('par')
legend([hp1,hp2,hp3(1)], 'Location','best')
If you do not have the Statistics and Machine Learning Toolbox (for fitnlm), an alternative would be the fminsearch function —
xest = fminsearch(@(x) norm(dados.par - f(x,dados.mass)), x0)
figure
hp1 = plot(dados.mass, dados.par, 'b.', 'DisplayName','Data');
hold on
hp2 = plot(dados.mass, f(xest,dados.mass), 'DisplayName','Regression');
hold off
grid
xlabel('mass')
ylabel('par')
legend([hp1,hp2], 'Location','best')
.
0 commentaires
Sam Chak
le 11 Août 2023
I have fixed the function f in the code. Now it should be working correctly. The dados.mass and dados.par data are not provided. Thus I made up some value to test the code.
mass = linspace(110, 190, 19);
par = linspace(0.1, 0.19, 19);
yv = 1:1:19;
for k1 = 1:length(yv)
% y = yv(k1); % unused
f = @(x) exp(-0.5*((x - 45)/mass(k1)).^2) - par(k1); % <-- fix it here
x(k1) = fzero(f, 0.5);
end
% Solutions
x
subplot(2,1,1)
plot(mass, x), xlabel('mass'), ylabel('x'), grid on
subplot(2,1,2)
plot(par, x), xlabel('par'), ylabel('x'), grid on
0 commentaires
Voir également
Catégories
En savoir plus sur Introduction to Installation and Licensing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



