Cant call function in script, function has switch case

4 Ansichten (letzte 30 Tage)
Chris Hansen
Chris Hansen am 16 Feb. 2019
Kommentiert: Walter Roberson am 16 Feb. 2019
So here is my function, Im trying to do a switch case for a certain n-value
function [wi,na] = weight_natural(n_poly)
switch (n_poly)
case n_poly==1;
wi=2;
na=0;
case n_poly==2;
wi(1)=1;
wi(2)=1;
na(1)=-0.577350269190;
na(2)=0.577350269190;
case n_poly==3;
wi(1)=(5/9);
wi(2)=(8/9);
wi(3)=(5/9);
na(1)=-0.774596669241;
na(2)=0;
na(3)=0.774596669241;
case n_poly==4;
wi(1)=0.347854845137;
wi(2)=0.652145154863;
wi(3)=0.652145154863;
wi(4)=0.347854845137;
na(1)=-0.861136311594;
na(2)=-0.339981043585;
na(3)=-na(2);
na(4)=-na(1);
case n_poly==5;
wi(1)=0.236926885056;
wi(2)=0.478628670499;
wi(3)=0.568888888889;
wi(4)=wi(2);
wi(5)=wi(1);
na(1)= -0.906179845939;
na(2)= -0.538469310106;
na(3)= 0;
na(4)=-na(2);
na(5)-na(1);
end
end
My main script
clc
clear all
format short G
%Project one
read=dlmread('data.txt');
number_pts=read(1,1);% read total number of elements
starting_pt=read(1,2);% read starting global point
length=read(1,3);% total length of element
x_dist=read(2,:);% total element length
Ci=read(3,:);% coefficients
nodes=number_pts + 1;% number of node, between elements
n_poly = 3 ;
[wi,na] = weight_natural(n_poly)
j=1;
for k = -5: x_dist
x_value(1,j) = (((starting_pt + x_dist(j)))/2) + (((starting_pt - x_dist(j)))/2)*wi
j = j + 1;
end

Antworten (1)

Steven Lord
Steven Lord am 16 Feb. 2019
Eliminate the "n_poly==" part of your case statements. MATLAB will perform the equality testing for you. See the "Compare Single Values" example on the documentation page for the switch keyword and use it as a model.
  1 Kommentar
Walter Roberson
Walter Roberson am 16 Feb. 2019
This is the better solution. However, another potential solution is to change the
switch (n_poly)
to
switch true
and leave all of the n_poly== where they are.
switch true is obscure but legal. I do not recommend it, but it is possible.

Melden Sie sich an, um zu kommentieren.

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by