The code indicates error with response Invalid value for OPTIONS parameter Algorithm
2 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
% Physics-Informed Neural Network (PINN) for 1D Poisson Equation
clear; clc;
% Define training data
N_f = 100; % Collocation points for PDE
x_f = rand(N_f,1); % Collocation points in (0,1)
N_b = 2; % Boundary points
x_b = [0; 1]; % Dirichlet boundary
% Neural network architecture
layers = [1, 20, 20, 1]; % [input, hidden1, hidden2, output]
% Initialize weights and biases
params = initializeNN(layers);
% Train the PINN using fmincon
lossFunc = @(p) PINNLoss(p, x_f, x_b);
options = optimoptions('fmincon','MaxIterations',500,'Display','iter','Algorithm','quasi-newton');
params_opt = fmincon(lossFunc, params, [], [], [], [], [], [], [], options);
% Predict using trained network
x_test = linspace(0,1,100)';
u_pred = neuralNet(x_test, params_opt, layers);
% Exact solution
u_exact = sin(pi*x_test);
% Plot
figure;
plot(x_test, u_pred, 'r--', 'LineWidth', 2); hold on;
plot(x_test, u_exact, 'b-', 'LineWidth', 2);
legend('PINN Prediction','Exact Solution');
xlabel('x'); ylabel('u(x)');
title('PINN vs Exact Solution');
grid on;
%% --- Helper Functions ---
function params = initializeNN(layers)
params = [];
for i = 1:length(layers)-1
W = randn(layers(i+1),layers(i));
b = randn(layers(i+1),1);
params = [params; W(:); b(:)];
end
end
function y = neuralNet(x, params, layers)
idx = 1;
a = x';
for i = 1:length(layers)-1
in = layers(i);
out = layers(i+1);
W = reshape(params(idx:idx+out*in-1), out, in);
idx = idx + out*in;
b = reshape(params(idx:idx+out-1), out, 1);
idx = idx + out;
a = W*a + b;
if i < length(layers)-1
a = tanh(a); % Activation
end
end
y = a';
end
function loss = PINNLoss(params, x_f, x_b)
layers = [1, 20, 20, 1];
% PDE loss at collocation points
u = @(x) neuralNet(x, params, layers);
du = @(x) gradient(u, x);
d2u = @(x) gradient(du, x);
f = @(x) pi^2 * sin(pi*x);
pde_loss = mean((d2u(x_f) + f(x_f)).^2);
% Boundary loss
u_b = u(x_b);
bc_loss = mean(u_b.^2);
loss = pde_loss + bc_loss;
end
function df = gradient(f, x)
h = 1e-4;
df = (f(x + h) - f(x - h)) / (2*h);
end
1 commentaire
Torsten
le 11 Juin 2025
You are correct. As you can see, "quasi-newton" is not a valid algorithm for "fmincon":
options = optimoptions('fmincon')
Réponses (1)
Walter Roberson
le 11 Juin 2025
quasi-newton is a valid option for fminunc but not for fmincon
0 commentaires
Voir également
Catégories
En savoir plus sur Sequence and Numeric Feature Data Workflows 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!