Problem with fplot - doesnt plot anything but when plotting the data individually it works

4 次查看(过去 30 天)
Using this code, the aim is to calcuate the magnetic field produced by a solenoid within a 3D space. However, the fplot isnt working with my function, but when I substitute values into the fucntion, I get data points that make sense, and this is plotted using the for loop. Is anyone able to help fix this problem? As its an analytical solution, a function must be used to describe it. Thanks
The Code:
magnet.length = 0.25;
magnet.radius = 0.03;
magnet.position = [0, 0, 0];
magnet.I = 2.50E+01;
sphere.R = 0.25;
magnet.half_length = magnet.length / 2;
mu0 = 4*pi*1e-7;
syms x x1 % constants
f_magnet(x) = (mu0 * magnet.I * (magnet.radius^2)) / (2 * ((x-x1-magnet.position(1))^2 + magnet.radius^2)^(3/2)) * 1 / magnet.length; % Biot-Savart law
B_x_magnet(x) = int(f_magnet(x), x1, -magnet.half_length, magnet.half_length); % magnitude of B within the coil (uniform)
figure()
fplot(B_x_magnet(x), [-3*sphere.R 3*sphere.R])
hold off
figure()
for x = -3*sphere.R : 0.01 : 3*sphere.R
plot(x, B_x_magnet(x), 'o')
hold on
end
When plotting data using for loop:
When plotting using fplot:
  2 个评论
Dyuman Joshi
Dyuman Joshi 2023-12-18
There are many undefined parameters in the code, so we can not run your code to reproduce the result you got.
Matt
Matt 2023-12-18
编辑:Matt 2023-12-18
Hello, thanks for your reply, yes I have just realised that magnet.half_length and mu0 isnt included in the parameters.
magnet.half_length = magnet.length / 2;
mu0 = 4*pi*1e-7;
Thanks

请先登录,再进行评论。

采纳的回答

Dyuman Joshi
Dyuman Joshi 2023-12-18
You will have to define x as a real symbolic variable (or assume that the symbolic variable 'x' is a real number).
magnet.length = 0.25;
magnet.radius = 0.03;
magnet.position = [0, 0, 0];
magnet.I = 2.50E+01;
sphere.R = 0.25;
magnet.half_length = magnet.length / 2;
%% not defined, so value is assumed
mu0 = 1;
%% Define x as a real symbolic variable
syms x real
syms x1 % constants
f_magnet(x) = (mu0 * magnet.I * (magnet.radius^2)) / (2 * ((x-x1-magnet.position(1))^2 + magnet.radius^2)^(3/2)) * 1 / magnet.length % Biot-Savart law
f_magnet(x) = 
B_x_magnet(x) = int(f_magnet(x), x1, -magnet.half_length, magnet.half_length) % magnitude of B within the coil (uniform)
B_x_magnet(x) = 
figure()
fplot(B_x_magnet(x), [-3*sphere.R 3*sphere.R])
figure()
for x = -3*sphere.R : 0.01 : 3*sphere.R
plot(x, B_x_magnet(x), 'o')
hold on
end
  3 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

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

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by