Editing a function and then using fzero

2 次查看(过去 30 天)
I am trying to write a code that uses fzero on a function.
As I see it the only one way to edit and ran scripts on a function is by declaring it as syms type.
But syms does not work with fzero.
How should I approach it?
* I am trying to solve a nonlinear equation with two variables. So I have made two syms variables and wrote two function with those syms variables. So I get two syms functions. Then I use subs on two of the functions in order to have a one variable equation using those two functions (sub syms type to a scalar). I a adding the code.
clear
clc
%variables
u0 = 4*pi*10e-7;
Ms = 0.8;
gamma = 1;
Ku=1;
D = 0;
A = 1;
sinfei = sin(-pi/2);
delta=1;
syms d W
for m = 1:2
surfaceV(m) = (sin((pi/gamma)*W*m)/sinh((pi^2/gamma)*delta*m))^2*((1-exp(-(2*pi/gamma)*m*d))/m);
volumeV(m) = (sin((pi/gamma)*W*m)/cosh((pi^2/gamma)*delta*m))^2*((exp(-(2*pi/gamma)*m*d)+((2*pi/gamma)*m*d)-1)/m);
end
surfaceEnergy = (1/2)*u0*Ms^2*(1-2*W/gamma)^2+((2*pi*u0*Ms^2*delta^2)/(gamma*d))*sum(surfaceV);
volumeEnergy = ((2*pi*u0*Ms^2*delta^2*sinfei^2)/(gamma*d))*sum(surfaceV);
dW_surfaceEnergy = diff(surfaceEnergy,W);
dW_volumeEnergy = diff(volumeEnergy,W);
non_volumeORsurface_energy=-((2*delta*Ku+2*A/delta+pi*D*sinfei)/W^2); %Non volume nor surface energies for total energy solution
d_vector = double(linspace(100,200,3));
W_d=double(zeros(1,length(d_vector),'uint64'));
for m = 1:length(d_vector)
W_d(m)=double(fzero(subs(symfun(dW_surfaceEnergy+dW_volumeEnergy+non_volumeORsurface_energy,W),d,d_vector(m)),50));
end
  2 个评论
Torsten
Torsten 2022-4-8
As I see it the only one to edit and ran scripts on a function is by declaring it as sys type.
I don't know what you mean.

请先登录,再进行评论。

采纳的回答

Torsten
Torsten 2022-4-8
编辑:Torsten 2022-4-8
%variables
u0 = 4*pi*10e-7;
Ms = 0.8;
gamma = 1;
Ku=1;
D = 0;
A = 1;
sinfei = sin(-pi/2);
delta=1;
syms d W
for m = 1:2
surfaceV(m) = (sin((pi/gamma)*W*m)/sinh((pi^2/gamma)*delta*m))^2*((1-exp(-(2*pi/gamma)*m*d))/m);
volumeV(m) = (sin((pi/gamma)*W*m)/cosh((pi^2/gamma)*delta*m))^2*((exp(-(2*pi/gamma)*m*d)+((2*pi/gamma)*m*d)-1)/m);
end
surfaceEnergy = (1/2)*u0*Ms^2*(1-2*W/gamma)^2+((2*pi*u0*Ms^2*delta^2)/(gamma*d))*sum(surfaceV);
volumeEnergy = ((2*pi*u0*Ms^2*delta^2*sinfei^2)/(gamma*d))*sum(surfaceV);
dW_surfaceEnergy = diff(surfaceEnergy,W);
dW_volumeEnergy = diff(volumeEnergy,W);
non_volumeORsurface_energy=-((2*delta*Ku+2*A/delta+pi*D*sinfei)/W^2); %Non volume nor surface energies for total energy solution
expression = matlabFunction(dW_surfaceEnergy+dW_volumeEnergy+non_volumeORsurface_energy)
d_vector = linspace(100,200,30);
W_d=zeros(1,length(d_vector));
W0 = 1;
format long
for i=1:numel(d_vector)
d = d_vector(i);
W_d(i) = fzero(@(W)expression(W,d),W0);
error = expression(W_d(i),d)
W0 = W_d(i);
end
The solutions don't differ much - but they are solutions as can be seen by error.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 General Physics 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by